2
這是我的類代碼MVC下拉列表不驗證,並給予警告
public string tempHouseHoldSSNVerification { get; set; }
這是我CSHTML代碼
<tr>
<td width="550"><p class="text-primary">Was an error found for SSN verification?</p></td>
<td width="20"></td>
@{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
{
Text = "-- Select One -- ",
Value = null,
Selected = true
});
listItems.Add(new SelectListItem
{
Text = "N/A",
Value = "N/A"
});
listItems.Add(new SelectListItem
{
Text = "Yes",
Value = "Yes"
});
listItems.Add(new SelectListItem
{
Text = "No",
Value = "No"
});
}
<td>
@Html.DropDownListFor(model=>model.tempHouseHoldSSNVerification, listItems, new { @class = "form-control" })
@Html.ValidationMessageFor(model=>model.tempHouseHoldSSNVerification)
</td>
</tr>
我想,以確保用戶選擇N的三個選項之一/ A,是或否,否則會給用戶一個警告。
您需要通過添加'[Required]'屬性來製作所需的屬性。但是你應該刪除你的第一個'SelectListItem',並使用'@ Html.DropDownListFor(m => m.tempHouseHoldSSNVerification,listItems,「 - Select One - 」,new {@class =「form-control」}) –