This is an MVC function that associates empty strings with null s.
This logic is controlled using the ModelMetadata.ConvertEmptyStringToNull property, which is used by DefaultModelBinder .
You can set ConvertEmptyStringToNull using the DisplayFormat attribute
public class OrderDetailsModel { [DisplayFormat(ConvertEmptyStringToNull = false)] public string Comment { get; set; }
However, if you do not want to comment on all the properties, you can create a custom mediator 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 ModelBinderAttribute in your action:
public ActionResult SaveOrderDetails([ModelBinder(typeof(EmptyStringModelBinder))] OrderDetailsModel orderDetailsModel) { }
Or you can set it as the default global ModelBinder in your Global.asax:
ModelBinders.Binders.DefaultBinder = new EmptyStringModelBinder();
Read more about this feature here .
nemesv 04 Oct 2018-12-12T00: 00Z
source share