任何想法爲什麼下面的下拉列表不會驗證必填字段爲int類型(下面的「標題」字段)?MVC3 DropDownList無法通過[必需的]類字段驗證爲int類型嗎?
[Required] // This works!
[Display(Name = "Name")]
public string Name { get; set; }
[Required] // This doesn't work
[Display(Name = "Title")]
public int TitleId { get; set; }
<div class="editor-label">
@Html.LabelFor(model => model.Name, "Name")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name, "This can't be blank!")
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TitleId, "Title")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.TitleId, (SelectList)ViewBag.TitleId, String.Empty)
@Html.ValidationMessageFor(model => model.TitleId)
</div>
當您發佈的數據,並TitleId已設定值? –
如果int?仍然無法工作,也許你爲你的下拉菜單設置了默認值?即:(0,「所有記錄」)而不是空值? – Iridio
不行...我把它設置爲「 - 選擇我 - 」,不幸的是沒有工作。 – JaJ