I am trying to show and edit a decimal for different cultures.
Example of the problem for Culture “de“: When initializing the decimal from the model with 1111,1 the view shows it correct like: 1.111,1 € When changing the value to: 1.123,00 € the controller interprets it after the POST as 1,123
How can I fix this problem in a nice and tidy way?
Model:
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;
public Class Foo
{
[DisplayName("Decimal")]
[Required(ErrorMessage = "Decimal is Required")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal MyDecimal { get; set; }
}
View:
@model Globalizer.Models.Foo
@{
using (Html.BeginForm("NewValues", "Home", FormMethod.Post))
{
@Html.ValidationSummary(true);
@Html.ValidationMessageFor(model => model);
@Html.EditorFor(model=>model)
<input type="submit" />
}
}
Controller: The controller just saves and loads the model.
Web config:
<system.web>
<globalization culture="auto" uiCulture="auto" />
</system.web>
I solved the problem partly with the next link:
https://johnnyreilly.github.io/jQuery.Validation.Unobtrusive.Native/AdvancedDemo/Globalize.html
The JQuery.Validation.Globalize package is available on NuGet.