我正在编码一个MVC 5互联网应用程序,我有一个问题关于视图模型中的字段验证。MVC 5模型验证消息不正确显示
这里是我的视图模型场:
[Display(Name = "Latitude")]
[Required(ErrorMessage = "Please enter a valid latitude.")]
public double startupLatitude { get; set; }
这里是我的视图代码:
<div class="form-group">
@Html.LabelFor(model => model.startupLatitude, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.startupLatitude, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.startupLatitude, "" , new { @class = "text-danger" })
</div>
</div>
如果我输入的值是不是双,如下列:
测试
我在视图中显示以下验证消息:
“测试”值对于“纬度”无效。
相反的:
请输入一个有效的纬度。
这是为什么?
在此先感谢。
添加'@ Html.EditorFor()'方法生成的html – 2014-12-03 06:26:41
此行为是正常且有效的,因为您正在输入字符串,而模型属性的类型为double。 – 2014-12-03 06:27:40
而'Required'的使用在这里没有意义,因为'double'必须总是有一个值。只有当你有'double?'(可为空) – 2014-12-03 06:31:39