I’m using $.ajax() to send a post call to the server. If I use POST for type, it gives an error, but if I do it with GET it works perfectly.
The problem is that when doing POST, the parameter unitTotal is null on the server-side. But when doing GET that parameter is correctly passed.
Here’s the ajax code:
$.ajax({
type: "POST",
url: '/Sepet/SepeteTabloEkle',
data: { "tabloId": tabloId, "en": en, "boy": boy, "accessoryIds": accessoryIds, "miscTypes": miscTypes, "unitTotal": total },
traditional:true,
success: function (data) {
},
});
Here’s my action definition:
[HttpPost]
public void SepeteTabloEkle(int tabloId, int en, int boy, List<int> accessoryIds, List<string> miscTypes, decimal unitTotal )
And here’s the error the server gives if I use POST. If I use GET this error is not thrown and unitTotal gets passed nicely.
The error:
The parameters dictionary contains a null entry for parameter
‘unitTotal’ of non-nullable type ‘System.Decimal’ for method ‘Void
SepeteTabloEkle(Int32, Int32, Int32,
System.Collections.Generic.List1[System.Int32],1[System.String], System.Decimal)’ in
System.Collections.Generic.List
‘RenkliTablo.Controllers.SepetController’. An optional parameter must
be a reference type, a nullable type, or be declared as an optionalparameter. Parameter name: parameters
That’s because of a difference in the culture from the client and the server. When using GET request the default model binder always uses
InvariantCultureto parse query string parameters into their underlying types as discussed here. When using POST the model binder uses the current culture. So if you are using a culture on the server where the decimal separator is,and not.the POST request fails.