I'm trying to send an Ajax POST using FormData.
var data = new FormData();
data.append('id', 1);
data.append('description', null);
$.ajax({
type: 'POST',
url: '/TestController/UpdateDescription',
data: data,
contentType: false,
processData: false,
success: [...]
});
In the controller I have:
[HttpPost]
public JsonResult UpdateDescription(int id, string description)
{
//description = "null", instead of null.
bool isDescriptionNull = String.IsNullOrEmpty(description); //false!
}
I'm using the same code in a different .NET 4.7 project and this does not happens, and I get a null value for description.
What's happening here?
Any value passed into data.append will be converted to a string. The only way to accomplish sending a null value is the send an empty string. i.e. data.append('description', '');
This will send a null value to the backend without stringifying it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With