我正在編碼一個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