简体   繁体   中英

How do you post and validate Globalized currency in Asp.net MVC?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM