Search code examples
c#nancy

Why does the model binding not bind an empty string thereby letting the property be null in Nancy (and others)?


In the Nancy code you have this in DefaultBinder on line 181

private bool BindingValueIsValid(string bindingValue, object existingValue, BindingMemberInfo modelProperty, BindingContext bindingContext)
{
    return (!String.IsNullOrEmpty(bindingValue) &&
           (IsDefaultValue(existingValue, modelProperty.PropertyType) ||
           bindingContext.Configuration.Overwrite));
}

When sending across empty strings as values sure enough my properties are being set (or rather not being set at all) to null. To me this should not be the default behavior as I do want to have a distinction between empty strings and nulls. There's also no option to change this at least that I can tell.

Any work arounds? Why does this seem to be the default behavior in Nancy, and ASP.NET MVC and others?


Solution

  • Seems to be a bug. I've submitted a pull request:

    https://github.com/NancyFx/Nancy/pull/2001