2

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; }
}
4
  • You were passing empty string not null value. Commented Sep 7, 2018 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? Commented Sep 7, 2018 at 13:13
  • You should pass null value or can use property attribute if value is empty then it will be null Commented Sep 7, 2018 at 13:17
  • possible duplicate of stackoverflow.com/questions/51376618/… Commented Sep 7, 2018 at 13:18

2 Answers 2

0

Add this to your PERSON model for firstName/lastName:

[DisplayFormat(ConvertEmptyStringToNull = false)]
... the model values 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

1 Comment

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 (learn.microsoft.com/en-us/aspnet/core/mvc/models/…), but I don't know see how to modify it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.