简体   繁体   English

您如何在Asp.net MVC中过帐和验证全球化货币?

[英]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 文化“ de”问题的示例:用1111,1初始化模型中的小数时,视图显示正确,如:1.111,1€将值更改为:1.123,00€控制器将POST解释为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 https://johnnyreilly.github.io/jQuery.Validation.Unobtrusive.Native/AdvancedDemo/Globalize.html

The JQuery.Validation.Globalize package is available on NuGet. NuGet上提供了JQuery.Validation.Globalize包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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