comma decimal seperator in asp.net mvc 5

11,004

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. :)

Share:
11,004
raphi011
Author by

raphi011

Updated on July 30, 2022

Comments

  • raphi011
    raphi011 almost 2 years

    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

    • Admin
      Admin almost 9 years
      What is the issue you having. Are you getting a client side validation error (and the form is not submitting)
    • raphi011
      raphi011 almost 9 years
      as i said the get request is called in my controller so as far as i know it can't be a client side validation error? correct me if im wrong
    • Admin
      Admin almost 9 years
      So what is the problem? - you have not shown your view or your controller methods or indicated where your error (is there one?) occurs. What is your actual question?
    • raphi011
      raphi011 almost 9 years
      i edited my original question to be more clear
    • Admin
      Admin almost 9 years
      Can you temporarily disable client side validation (in the view add @ { HtmlHelper.ClientValidationEnabled = false; HtmlHelper.UnobtrusiveJavaScriptEnabled = false; } to confirm this is a server side issue.
    • raphi011
      raphi011 almost 9 years
      disabled - and still not working.
    • Admin
      Admin almost 9 years
      I am not able to reproduce your issue. Do you have any custom model binders?
    • raphi011
      raphi011 almost 9 years
      no i do not! the problem is i don´t know how to debug the default model binder, so i can find out why the parsing isn't working ..
    • raphi011
      raphi011 almost 9 years
  • raphi011
    raphi011 almost 9 years
    it is in germany / austria. this is a localization issue.
  • Hemant Bhagat
    Hemant Bhagat almost 9 years
    that means you want to accept the value as 3,0.. Ryt?? Then please see my edit now.. May this help up.
  • raphi011
    raphi011 almost 9 years
    weird that this doesn't work out of the box ... but thanks for the answer! i was going crazy.
  • Geeky Guy
    Geeky Guy over 7 years
    You are switching languages on every request made to your app if you do that.