Decimal symbol issue in MVC, C#?

半腔热情 提交于 2019-12-06 16:02:20

You are getting a client side validation error as a result of jquery.validate.js which uses the following to validate the value (which only allows the . character as the decimal separator.

number: function(value, element) {
    return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
},

You can use the jquery.validate.globalize.js plugin (refer this article for more detail) or you can add your own script to modify the validator, for example (include this after jquery.validate.js)

$.validator.methods.number = function (value, element) {
   return this.optional(element) || $.isNumeric($(element).val().replace('/', '.'));
}

Try to set the view format explicitly

@Html.DisplayFor(x => string.Format("{0:0.00}", x.NbatPersent));

else you can write a custom editor template for the double type (~/Views/Shared/EditorTemplates/double.cshtml):

@model double?
@Html.TextBox("", Model.HasValue ? Model.Value.ToString("#,##0.000#") : "")

and then in your view:

@Html.EditorFor(x => x.NbatPersent)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!