簡體   English   中英

Aspnet MVC 4驗證十進制數

[英]Aspnet MVC 4 validating decimal numbers

我在應用程序中遇到一些驗證小數的問題。 例如,如果我在文本框中寫入“14,25”,這對我的文化來說是正確的,那么它將無法通過客戶端驗證。 此外,如果我寫入14.25,則刪除點並將1425放置在實體屬性中,此外,在SaveChanges之后,保存到數據庫表的值為999,99。 需要就此問題提出建議。 這與我在這里的另一個問題有關: https//stackoverflow.com/questions/24186365/set-jquery-culture-once-for-the-entire-aplication-possible/24186535#24186535

編輯 :代碼添加,它就這么簡單,我認為我沒有必要,因為這必須是一個常見的問題。

[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>

嘗試使用http://github.com/jquery/globalize中的 jQuery全球化插件。

將以下文件添加到/ scripts文件夾:

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

在BundleConfig.cs中:

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

在_Layout.cshtml中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM