Question

I'm building a website based on ASP.NET MVC 4 C#. I have encountered a problem when using @Html.EditorFor(model => model.Weight) when weight is a double. If I enter only numbers into the textfield ModelState.IsValid returns true. If I enter numbers separated by a comma, the client side validations says that this is not a valid number. If I enter numbers separated by a dot, the client side validations is ok, but on server side the ModelState.IsValid returns false.

This is the model I want to edit (generated by entity framework, based on database table):

using System;
using System.Collections.Generic;

public partial class Record
{
    public int Id { get; set; }
    public int ExerciseId { get; set; }
    public double Weight { get; set; }
    public System.Guid UserId { get; set; }
    public System.DateTime CreatedDate { get; set; }

    public virtual Exercise Exercise { get; set; }
}

My View

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

    <div class="editor-field">
        @Html.DropDownList("ExerciseId")
        @Html.ValidationMessageFor(model => model.ExerciseId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Weight)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Weight) //this is the issue
        @Html.ValidationMessageFor(model => model.Weight)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.CreatedDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.CreatedDate)
        @Html.ValidationMessageFor(model => model.CreatedDate)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

I've tried to follow this solution by creating my own model binder, but I can't get this to work.

DecimalModelBinder.cs

 using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TrainingLog.Helper
{
    public class DecimalModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            var modelState = new ModelState { Value = valueResult };
            object actualValue = null;
            try
            {
                actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.InvariantCulture);
            }
            catch (FormatException e)
            {
                modelState.Errors.Add(e);
            }

            bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
            return actualValue;
        }
    }

    public class EFModelBinderProvider : IModelBinderProvider
    {
        public IModelBinder GetBinder(Type modelType)
        {
            if (modelType == typeof(decimal))
            {
                return new DecimalModelBinder();
            }
            return null;
        }
    }
}

Line added in Global.asax.cs Application_Start():

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
Was it helpful?

Solution

Your Weight property is of type double, but you have created a model binder for the type decimal.

Change your model binder to this:

public class DoubleModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        object actualValue = null;
        try
        {
            actualValue = Convert.ToDouble(valueResult.AttemptedValue, CultureInfo.InvariantCulture);
        }
        catch (FormatException e)
        {   
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, e);
        }    
        return actualValue;
    }
}

And, in your Global.asax.cs's Application_Start():

ModelBinders.Binders.Add(typeof(double), new DoubleModelBinder());

You don't need the EFModelBinderProvider. You can just remove that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top