In ASP.NET Core 2.1, I'm noticing basically the opposite is happening to this question: string.empty converted to null when passing JSON object to MVC Controller
When I send a JSON
object back to a controller, that has properties with empty strings, they are no longer automatically being converted to null
.
For example, take this JS object:
var person = {firstName:"", lastName:""};
$http.post("/app/index", person)
.then(function (response) {
// Success
}, function (error) {
// Failure
})
When bound to the model in the controller, the properties with empty strings are remaining as empty strings:
//
// POST: /App
[HttpPost]
public async Task<ActionResult> Index([FromBody] AppFormDTO data)
{
....
}
Which when saving this object to the database, I'd prefer to have null
values instead of a bunch of empty strings.
Did they change the DefaultModelBinder
in ASP.NET Core 2.1
? And if so, how do I change it back to how it was in MVC5 -- where string.empty
was automatically converted to null
?
UPDATE, to add my AppFormDTO
model, for reference:
public class AppFormDTO
{
public string firstName{ get; set; }
public string lastName{ get; set; }
}