globalization - How to set decimal separators in ASP.NET MVC controllers? -


I am trying to teach myself ASP.Net MVC. However, I have stumbled over a problem with globalization, where my server presents a floating point number with a comma in the form of a decimal point, but they need dots in the virtual earth map, which creates some problems.

Resolved, but if I now try to post an edited dinner entry with dots, then the decimal separator fails as a decimal separator ( InvalidOperationException Throwing) when updating the model ( UpdateModel () in metod). I think that I have to establish a proper culture anywhere in the controller, I tried to do it in OnActionExecuting () , but he did not help.

I have revisited this issue in a real project and finally found a working solution . A custom model is bundled for the appropriate resolution for decimal (and decim?? />> If you are using them):

  Public class decimal modellbender: default modelbinders {public override object bund modal (controller contact controller contex, modelbinding context binding content) {item result = null; // Do not do it here! // It can be binding. Consort ModelState. AddModelError // and there is no ExitModel error! // // results = base. Bind Modal (Controller Consultant, Binding Contact); String ModelName = Binding Containing Box.ModelName; Attempted string = value = binding contentbox. Value provider Gate value (modelname) Used value; // Depending on the cultureInfo, the number can be called "," or "". // Both "." And "," should be accepted, but are not. String desired separator = number format inf ... Current enphonenet Decimalseparator; String optional sheller = (wantedSeperator == ","? "." ":", "); If (attempted value.InxOf (wantedSeperator) == -1 & amp; amp; amp; Attempted Values.InxOfOf (Optional Ceprator) = 1) {tryedValue = triedValue.Replace (alternateSeperator, wantedSeperator), try {if (binding contactx.metal metadata.usableable value type & amp; strings.isational whiteSpace Value)) {return null;} result = decimal. Purse (attempted value, number style. Any);} hold (format eclipse (E); adodel (model name, e);} return result;}}  

then in Application_Start () in Global.asax.cs:

Pre> ModelBinders.Binders.Add (typewrite, new decimalsModelBand ()); ModalBinders.binders.add (typef (decimal?), New decimalModelBender ());


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -