简体   繁体   English

Aspnet MVC 4验证十进制数

[英]Aspnet MVC 4 validating decimal numbers

I'm facing some problems validating decimals in my application. 我在应用程序中遇到一些验证小数的问题。 For example, if I write "14,25" into the textbox, which is correct for my culture, it fails client side validation. 例如,如果我在文本框中写入“14,25”,这对我的文化来说是正确的,那么它将无法通过客户端验证。 Also if i write 14.25 the dot is removed and 1425 is placed in the entity property and moreover, after SaveChanges the value saved to database table is 999,99. 此外,如果我写入14.25,则删除点并将1425放置在实体属性中,此外,在SaveChanges之后,保存到数据库表的值为999,99。 Need advide on this issue. 需要就此问题提出建议。 This is related to my other question here: https://stackoverflow.com/questions/24186365/set-jquery-culture-once-for-the-entire-aplication-possible/24186535#24186535 这与我在这里的另一个问题有关: https//stackoverflow.com/questions/24186365/set-jquery-culture-once-for-the-entire-aplication-possible/24186535#24186535

EDIT : Code added, it is as simple as this, I thought I wouldn't be necessary as this have to be a common issue. 编辑 :代码添加,它就这么简单,我认为我没有必要,因为这必须是一个常见的问题。

[DisplayFormat( DataFormatString = "{0:n2}", ApplyFormatInEditMode = true )]
        public decimal Peso { get; set; }

@Html.LabelFor(model => Model.Peso)
<div class="input-control text" data-role="input-control">
    @Html.EditorFor(model => Model.Peso)
    @Html.ValidationMessageFor(model => Model.Peso)
</div>

Try using jQuery's Globalization plugin from http://github.com/jquery/globalize . 尝试使用http://github.com/jquery/globalize中的 jQuery全球化插件。

Add following files to /scripts folder: 将以下文件添加到/ scripts文件夹:

/scripts/globalize.js
/scripts/cultures/globalize.cultures.js
/sctipts/cultures/globalize.culture.de-DE.js

In BundleConfig.cs: 在BundleConfig.cs中:

bundles.Add(new ScriptBundle("~/bundles/scripts/globalization").Include(
      "~/Scripts/globalize*",
      "~/Scripts/cultures/globalize*"));

In _Layout.cshtml: 在_Layout.cshtml中:

@Scripts.Render("~/bundles/scripts/globalization")

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

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