我想为文本框和下拉列表实现必填字段的验证。这是代码: 对于文本框:
@Html.Label("Part/Location", new {@class = "control-label"})
@Html.TextBox("PartLocation", null, new { @class = "form-control", @required = "required" })
for Dropdownlist:
@Html.Label("Location", new {@class = "control-label"})
@Html.DropDownList("DirectionTypes", null, "-- Select Direction --", new { @class = "form-control", @required = "required" })
当我单击按钮提交而不在文本框中键入任何输入并在下拉列表中选择时,文本框会调用input-validation-error并在文本框周围有棕色边框,但下拉列表不会。两者都具有属性required =" required"。那么,我如何才能对下拉列表产生相同的效果呢?
答案 0 :(得分:1)
在包含DirectionTypes
的模型中,请考虑添加int
密钥(可能是DirectionTypeID
?):
[Required]
public int DirectionTypeID { get; set; }
然后您可以使用@Html.DropDownListFor
之类的:
@Html.DropDownListFor(m => m.DirectionTypeID,
DirectionTypes,
"-- Select Direction --",
new { @class = "form-control", @required = "required" })