Search code examples
c#jqueryasp.netasp.net-mvcglobalization

comma decimal seperator in asp.net mvc 5


im desperately trying to make asp.net work with the comma symbol as the decimal seperator but this seems to be a lot harder then necessary...

i've done everything that's in this tutorial http://www.asp.net/mvc/overview/getting-started/introduction/examining-the-edit-methods-and-edit-view

tried this in the root web config

<system.web>
    <globalization culture="de-DE" uiCulture="de-DE" />
</system.web>

stepped through the jQuery code - the globalization there seems to work.

i'm using a get request with a model view Controller that looks like this

public class SearchCalcViewModel
{
        public SearchCalcViewModel() { }

        public IEnumerable<Calculation> Calculations { get; set; }
        [Display(Name="Name")]
        public string Name { get; set; }
        [Display(Name="Height")]
        public decimal? Height { get; set; }
}

the get request is called in the in the maincontroller - so that strengthens my assumption that the jquery culture dependent validation is working and something in the .net culture is awry even though Thread.CurrentTHread.CurrentCulture / CurrentUICulture is set correctly too.

When i try to fill in 3,0 as a height I get the following error message:

The value '3,0' is not valid for Height.

This is the import part of my view:

@using (Html.BeginForm("Search", "Main", FormMethod.Get))

<div class="form-group">
         @Html.LabelFor(m => m.Height, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
             @Html.TextBoxFor(m => m.Height, new { @class = "form-control"})
             @Html.ValidationMessageFor(m => m.Height)
        </div>
     </div>
}

this is my MainController:

public ActionResult Search(SearchCalcViewModel searchViewModel)
    {
        searchViewModel.Products = db.Products;
        searchViewModel.Calculations = from c in db.Calculations select c;


        if (searchViewModel.Height.HasValue)
        {
            searchViewModel.Calculations =  searchViewModel.Calculations.Where(c => c.Length == searchViewModel.Height);
        }


        return View(searchViewModel);
    }

i've stepped into the modelstate and somehow the culture is different from my current culture

wrong culture


Solution

  • Your value is 3,0 which is not a valid decimal type value. It should be 3.0 replace " comma(,) with dot(.).

    Edit : Create your own model binder.

    public class DecimalModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
    
            return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(valueProviderResult.AttemptedValue);
    
        }    
    }
    

    Add these lines in Application_Start file.

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

    I think this should work now. :)