1

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; }
}
Daniel Congrove
  • 3,519
  • 2
  • 35
  • 59
  • You were passing empty string not null value. – Md. Abdul Alim Sep 07 '18 at 13:11
  • Right, but in ASP.NET MVC5, when an empty string was passed, it was automatically converted to null. I'm converting an MVC5 project to ASP.NET Core 2.1, and I'm wondering if I can retain that feature in the new framework? – Daniel Congrove Sep 07 '18 at 13:13
  • You should pass null value or can use property attribute if value is empty then it will be null – Md. Abdul Alim Sep 07 '18 at 13:17
  • possible duplicate of https://stackoverflow.com/questions/51376618/asp-net-core-mvc-empty-string-to-null-when-sending-json-to-server – Antoine V Sep 07 '18 at 13:18

3 Answers3

0

Add this to your PERSON model for firstName/lastName:

[DisplayFormat(ConvertEmptyStringToNull = false)]
... the model values 
Jebathon
  • 4,310
  • 14
  • 57
  • 108
  • 2
    I've tried adding this set to both `false` and `true`, doesn't seem to make a difference, it's still coming through as an empty string instead of null. – Daniel Congrove Sep 07 '18 at 13:25
0

Following the source code of ASP.NET Core

 if (bindingContext.ModelType == typeof(string))
 {
      // Already have a string. No further conversion required but handle ConvertEmptyStringToNull.
      if (bindingContext.ModelMetadata.ConvertEmptyStringToNull && string.IsNullOrWhiteSpace(value))
      {
           model = null;
      }
      else
      {
           model = value;
      }
 }

The behavior by default is set to empty string to null. Recheck your model AppFormDTO if you putted ConvertEmptyStringToNull = false which is not correct.

Antoine V
  • 6,998
  • 2
  • 11
  • 34
  • I added my `AppFormDTO` model for reference, which doesn't use the `ConvertEmptyStringToNull` attribute. Are you able to get it to work correctly? I'm thinking default model binding for a JSON object is overriding this. Specifically the `JSONInputFormatter` mentioned in the docs (https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1), but I don't know see how to modify it. – Daniel Congrove Sep 07 '18 at 15:12
-1

double check if the json object has those properties as null and not as empty string...

user5919789
  • 35
  • 2
  • 4