When posting input data as a FormData to the ASP.NET Core MVC controller, by default empty string values are coerced to null values.
However, when sending input data as JSON to the controller, empty string values remain as they are. This leads to different behavior when validating string properties. For example, description field is not bound to null, but to empty string on the server:
{
value: 1,
description: ""
}
This in turn makes following model invalid, even though Description is not required:
public class Item
{
public int Value { get; set; }
[StringLength(50, MinimumLength = 3)]
public string Description { get; set; }
}
This is contrary to the behavior when same data is submitted via form.
Is there a way to make model binding of JSON behave in the same way as model binding of form data (empty string coerce to null by default)?