更新 07.01.2018
尽管有人建议这是一个 jQuery 问题而不是 MS ASP MVC 问题,但我认为这是一个 MVC 问题。我在 asp.net core 2.0 MVC 中创建了整个应用程序,但错误仍然存在。对我来说,将它与 MVC 联系起来的事实是,我可以通过添加以下行来解决日期验证问题:[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
到模型。因此,MVC 对验证有影响。所以我假设 MVC 中有一些方法可以解决这个问题(参见 post )。请发布 asp.net core 2.0 的答案。
原帖
在 MVC5 页面中,我在文本框中呈现一个 Double
属性。加载页面时,该值显示为带有“,”作为小数点分隔符,这是正确的,因为该页面在德语系统上运行。如果我想保存表单,我会收到验证错误。如何解决?我知道关于这个话题有一些问题,但据我所知,其中大部分都已经过时了……我还在苦苦挣扎,没有任何设置或任何内置的东西可以让来自不同国家的用户使用 MVC 应用程序。
型号:
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public Double Gewicht
{
get { return gewicht; }
set { gewicht = value; OnPropertyChanged(new PropertyChangedEventArgs("Gewicht")); }
}
CSHTML:
<div class="form-group">
@Html.LabelFor(model => model.Gewicht, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-8">
@Html.EditorFor(model => model.Gewicht, new { htmlAttributes = new { @class = "form-control col-md-1" } })
@Html.ValidationMessageFor(model => model.Gewicht, "", new { @class = "text-danger" })
</div>
</div>
Web.config
<globalization uiCulture="de-DE" culture="de-DE" />
单击提交按钮后的框 --> 相同值的验证错误
更新 05.01.2018
我已经尝试了显示的解决方案 here不幸的是,这对我不起作用。但是,我还发现,这些值不仅没有被接受,而且还被更改为混合了组分隔符和小数分隔符的数字(见图)。发生的情况是,22 的值更改为 22.5 并存储在数据库中。结果是,值 2,250.00 存储到数据库中。
更新 07.01.2018
同样有趣的是,日期字段完全可以接受德语格式。
属性
private DateTime? inbetriebnahmedatum;
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? Inbetriebnahmedatum
{
get { return inbetriebnahmedatum; }
set { inbetriebnahmedatum = value; OnPropertyChanged(new PropertyChangedEventArgs("Inbetriebnahmedatum")); }
}
更新 07.01.2018 - 2
如果我将 edit.cshtml 中的行从
<input asp-for="Gewicht" class="form-control" />
到
<input name="Gewicht" id="Gewicht" type="number" class="form-control" value="@Model.Gewicht"/>
可以使用值“23,7”提交表单而不会出现验证错误。在 Controller 中,绑定(bind)模型属性显示值“237”,而 IFormCollection
显示值“23.7”。这表明模型绑定(bind)器存在问题。
最佳答案
Even though it was suggested, that this is rather a jQuery problem than an MVC problem, I think it is an MVC Problem.
不,那是不正确的。您看到客户端验证错误是因为默认情况下 jquery.validate.js
(与 MicroSoft 无关的独立第三方插件,MVC 用于客户端验证)根据小数点分隔符为 .
验证数字(点),不是 ,
(逗号)。
MVC 是服务器端代码,不在浏览器中运行。要执行客户端验证,MVC 的 HtmlHelper
生成表单控件的方法呈现一组 data-val-*
html 中的属性用于描述要执行的验证,这些属性又由 jquery.validate.unobtrusive.js
解析加载 DOM 时的插件,并使用它们将规则添加到 $.validator
.
对于您的 double
属性它将添加一个 data-val-number
属性(除了 data-val-required
属性),这将添加 number
规则定义为
// http://docs.jquery.com/Plugins/Validation/Methods/number
number: function( value, element ) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
},
其中小数点分隔符是点,千位分隔符是逗号(估计是因为插件是美国开发的,所以使用的是美国格式)。
您需要覆盖默认行为,您可以使用诸如 jquery.globalize 之类的插件来做到这一点。 ,或包括以下脚本(注意正则表达式只是交换点和逗号)
$.validator.methods.number = function (value, element) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)?(?:,\d+)?$/.test(value);
}
注意上面的脚本需要在jquery.validate.js
之后脚本但未包含在 $(document).ready()
中
What links it to MVC for me, is the fact that I can solve the date validation problem by adding the line
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
to the model.
其实是你的[DataType(DataType.Date)]
属性与 [DisplayFormat]
结合使用影响生成的 html 的属性。 [DataType]
属性生成 <input type="date" ... />
如果浏览器支持,它又会呈现浏览器 HTML-5 日期选择器。根据规范,格式必须为 yyyy-MM-dd
(ISO 格式)因此需要 [DisplayFormat]
属性也是如此。
HTML-5 日期选择器以浏览器文化呈现日期。您在输入为 26.1.2018
的位置显示的图像是因为你的浏览器文化是de-DE
,但如果我导航到您的网站,我会看到 26/1/2018
在输入中,因为我的文化是 en-AU
(澳大利亚),如果美国用户导航到您的网站,他们会看到 1/26/2018
.
客户端验证对日期属性起作用的原因是 jquery.validate.js
插件包含美国格式(MM/dd/yyyy
)和 ISO 格式(yyyy-MM-dd
)的日期规则。
如果你使用 @Html.TextBoxFor(m => m.Inbetriebnahmedatum)
(忽略您的 [DataType]
和 [DisplayFormat]
属性),并输入 26.1.2018
在输入中,您还会看到客户端验证错误。
关于c# - MVC/JQuery 验证不接受逗号作为小数点分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48066208/