3

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

raphi011
  • 502
  • 1
  • 6
  • 23
  • What is the issue you having. Are you getting a client side validation error (and the form is not submitting) –  Jul 05 '15 at 10:24
  • 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 – raphi011 Jul 05 '15 at 10:28
  • 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? –  Jul 05 '15 at 10:31
  • i edited my original question to be more clear – raphi011 Jul 05 '15 at 10:45
  • 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. –  Jul 05 '15 at 10:54
  • disabled - and still not working. – raphi011 Jul 05 '15 at 11:03
  • I am not able to reproduce your issue. Do you have any custom model binders? –  Jul 05 '15 at 11:05
  • 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 Jul 05 '15 at 11:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82403/discussion-between-raphi011-and-stephen-muecke). – raphi011 Jul 05 '15 at 11:54

2 Answers2

3

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

Hemant Bhagat
  • 173
  • 11
0

I know is old but I had the same problem (with es-AR) and I found a better solution, you can simple do this:

void Application_AcquireRequestState(object sender, EventArgs e)
{
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(HttpContext.Current.Session["userCulture"]);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(HttpContext.Current.Session["userCulture"]);
}

On Global.asax

This code runs before model binding so you can set the culture information to the thread and also you have access to the session variable (for especific user culture)

Santiago
  • 2,190
  • 10
  • 30
  • 59