string.empty converted to null when passing JSON object to MVC Controller
Asked Answered
S

5

36

I'm passing an object from client to server. Properties of the object which are represented as string.empty are being converted to null during this process. I was wondering how to prevent this when the objects type supports string.empty.

enter image description here

console.log("DataToPost:", dataToPost);

$.ajax({
    type: "POST",
    contentType: 'application/json'
    url: "../../csweb/Orders/SaveOrderDetails/",
    data: dataToPost,
    success: function (result) {
        console.log(result);
    },
    error: function (e) {
        console.error(e);
    }
});

enter image description here

My model includes nullable DateTime objects. I cannot force all nulls to string.empty on the server.

I am using AutoMapper, so I would prefer not to have to inspect properties individually on the server.

Sherbet answered 4/10, 2012 at 19:3 Comment(2)
Try to stringify the data: data: JSON.stringify(dataToPost), and you should specify also the contentType to: contentType: "application/json"Kandi
@Kandi Thanks for your suggestions. The data still appears as null after applying JSON.stringify(dataToPost) and specifying the content type.Sherbet
K
74

This is a MVC feature which binds empty strings to nulls.

This logic is controlled with the ModelMetadata.ConvertEmptyStringToNull property which is used by the DefaultModelBinder.

You can set the ConvertEmptyStringToNull with the DisplayFormat attribute

public class OrderDetailsModel
{
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string Comment { get; set; }

    //...
}

However if you don't want to annotate all the properties you can create a custom model binder where you set it to false:

public class EmptyStringModelBinder : DefaultModelBinder 
{
    public override object BindModel(ControllerContext controllerContext,
                                     ModelBindingContext bindingContext)
    {
        bindingContext.ModelMetadata.ConvertEmptyStringToNull = false;
        Binders = new ModelBinderDictionary() { DefaultBinder = this };
        return base.BindModel(controllerContext, bindingContext);
    }
}

And you can use the ModelBinderAttribute in your action:

public ActionResult SaveOrderDetails([ModelBinder(typeof(EmptyStringModelBinder))] 
       OrderDetailsModel orderDetailsModel)
{
}

Or you can set it as the Default ModelBinder globally in your Global.asax:

ModelBinders.Binders.DefaultBinder = new EmptyStringModelBinder();

You can read more about this feature here.

Kandi answered 4/10, 2012 at 19:21 Comment(6)
This is clearly the correct answer to the problem. Thank you. Update: Works as advertised, too!Sherbet
If I set the ModelBinderAttribute-attribute for just a single parameter as described, the solution above does not work. It appears the standard DefaultModelBinder is used when binding the individual model properties, which makes them null anyway. My fix for this was to add the following code to the beginning of the overridden BindModel()-method: Binders = new ModelBinderDictionary() { DefaultBinder = this };Treacherous
Confirmed: the example above does not work applying attribute in actionRachal
@Treacherous thanks for the fix. I've update my post with the information.Kandi
using System.ComponentModel.DataAnnotations;Sensorimotor
Please see my recommended solution below, if this is not working for you (it did not work for me, as I would have liked).Mignonne
M
11

Instead of creating a ModelBinder which modifies the ModelMetadata as some answers suggested, a cleaner alternative is to provide a custom ModelMetadataProvider.

public class EmptyStringDataAnnotationsModelMetadataProvider : System.Web.Mvc.DataAnnotationsModelMetadataProvider 
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        modelMetadata.ConvertEmptyStringToNull = false;
        return modelMetadata;
    }
}

Then in Application_Start()

ModelMetadataProviders.Current = new EmptyStringDataAnnotationsModelMetadataProvider();
Masticate answered 1/12, 2013 at 14:39 Comment(2)
In MVC 5.2.3, ModelMetadataProviders.Current returns an instance of CachedDataAnnotationsModelMetadataProvider by default. The CachedDataAnnotationsModelMetadataProvider seals the CreateMetaData method so you can't override it like you are suggesting here. I'm guessing the cached provider is more performant, so I'm not sure if this answer is a good idea since it does not use the cached provider (and can't anyway).Tailgate
This is the only way to properly propagate ModelMetadata.Provider to instances created by the framework for properties of complex objects. The EmptyStringModelBinder can’t apply to properties of complex objects without itself being expanded to reimplement complex model binding. It is unfortunate that this subclassing style of extensibility prevents incremental extensibility. No support for mixing in multiple providers. But at least there is an extension point.Saul
M
3

The accepted answer did not work for me using MVC4. However, the following workaround does and I thought it would help others.

public class CustomModelBinder : DefaultModelBinder
{
    public bool ConvertEmptyStringToNull { get; set; }

    public CustomModelBinder ()
    {
    }

    public CustomModelBinder (bool convertEmptyStringToNull)
    {
        this.ConvertEmptyStringToNull = convertEmptyStringToNull;
    }

    protected override bool OnModelUpdating(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        // this little bit is required to override the ConvertEmptyStringToNull functionality that we do not want!

        foreach (string propertyKey in bindingContext.PropertyMetadata.Keys)
        {
            if(bindingContext.PropertyMetadata[propertyKey] != null)
                    bindingContext.PropertyMetadata[propertyKey].ConvertEmptyStringToNull = this.ConvertEmptyStringToNull;
        }
        return base.OnModelUpdating(controllerContext, bindingContext);
    }


}

This will fix the issue under MVC4+. It would seem that bindingContext.ModelMetadata.ConvertEmptyStringToNull is completely ignored, and this is because the setting exists in the PropertyMetadata object for each property being bound. PropertyMetadata is recreated in BindProperty() so if you set it before that method call it will get overwritten unless it exists as an attribute on the property of your object being bound (such as [DisplayFormat(ConvertEmptyStringToNull=false)]). No one wants to do this on every property as that's silly.

Mignonne answered 10/10, 2013 at 18:29 Comment(1)
I'm facing a similar problem with arrays. Json strings like "{ foo: [] }" result in foo=null when it comes to the parameters of mvc-actions. Is there any way to tweak the deserialization of the default mvc json-deserializer so that it will convert the given json-string to an actual empty array (aka not null)?Scottiescottish
M
0

The problem is that AutoMapper turns nullables unto null when string is empty. This other question was answered with something that I believe suffices your needs: Automapper null string to empty

Meteorograph answered 4/10, 2012 at 19:16 Comment(1)
Thanks for your response. If you'll view my second screenshot, you'll see that my watch window is hovering over the paramater passed into SaveOrderDetails. AutoMapper has not come into play just yet, but perhaps AutoMapper could solve the issue. I'll have a play with it.Sherbet
C
0

When posting data with $.ajax, null is not a possible value for a property of the data option. If you look at the request with an http debugger, you will see that it's converted to an empty string.

So I guess that your MVC controller is applying the opposite conversion.

What I do in an ajax application to workaroung this issue is that I don't use the data option of $.ajax(), but I serialize everything in JSON and put it into a single field "data" of the data option. Like that you don't have problems with null values. Of course, you have to deserialize on the server side.

Cuevas answered 4/10, 2012 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.