Articles in this section
Category / Section

How to prevent the default behavior of MVC converting String.Empty to null values?

1 min read

The default behavior of MVC to convert String.Empty to null values while binding model values from the client to server-end. Because of this, Empty String (“ ”) values formed in the filtered query will be changed to the null values while receiving them in the server-end.

To overcome this problem, DefaultModelBinder can be inherited and prevent the conversion of String.Empty to the null values.

Razor:

@using Syncfusion.JavaScript.Models
 
@(Html.EJ().Grid<OrdersView>("Grid")
    .AllowPaging()
    .Datasource(ds =>
    {
        ds.URL("/Home/UrlDataSource");
        ds.Adaptor(AdaptorType.UrlAdaptor);
    })
    .AllowFiltering()
    .FilterSettings(filter=>filter.FilterType(FilterType.Excel).BlankValue(""))
    .Columns(col =>
    {
        col.Field("OrderID").HeaderText("Order ID")
            .TextAlign(TextAlign.Right).Add();
        col.Field("EmployeeID").HeaderText("EmployeeID")
            .TextAlign(TextAlign.Right).Add();
        col.Field("Freight").HeaderText("Freight")
            .TextAlign(TextAlign.Right)
            .Format("{0:C}").Add();
        col.Field("CustomerID").HeaderText("Customer ID").Add();
        col.Field("ShipRegion").Type(ColumnType.String).HeaderText("ShipRegion").Add();
    })
)
 

 

 

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult UrlDataSource([ModelBinder(typeof(EmptyStringModelBinder))]DataManager dm)
        {
            IEnumerable DataSource = OrderRepository.GetAllRecords();
            DataOperations ds = new DataOperations();
            if (dm.Where != null && dm.Where.Count > 0)
            {
                DataSource = ds.PerformWhereFilter(DataSource, dm.Where, dm.Where[0].Operator);
            }
            var count = DataSource.Cast<EditableOrder>().Count();
            if ((dm.Skip == 0 || dm.Take == null) && (dm.Take == 0 || dm.Take == null))
                return Json(new { result = DataSource, count = count });
            DataSource = ds.PerformSkip(DataSource, dm.Skip);
            DataSource = ds.PerformTake(DataSource, dm.Take);
            return Json(new { result = DataSource, count = count });
        }
 
        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);
            }
        }
    }

 

Check the result after and before applying the Model binder.

Figure 1. Values before applying Model Binder

Figure 2. Values after applying Model binder

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied