请如何从Controller或View验证我的Dropdownlist是强制性的。我不想从模特那里做到。
查看
<div class="col-md-4">
<div>
@Html.LabelFor(model => model.item.OPTION_ID, "Parent", new { @class = "control-label" })
@{
List<SelectListItem> dropdownlist3 = new List<SelectListItem>();
dropdownlist3.Add(new SelectListItem { Text = "--Please Select Parent--", Value = "-1" });
foreach (var item in Model.list)
{
dropdownlist3.Add(new SelectListItem { Text = item.OPTION_VALUE, Value = item.OPTION_ID.ToString() });
}
@Html.DropDownListFor(m => m.item.PARENT_ID, dropdownlist3, new { @class = "form-control", @id = "dropdown", })
@Html.HiddenFor(n => n.item.PARENT_ID1, new { @id = "parentid1" })
@Html.HiddenFor(n => n.item.PARENT_ID2, new { @id = "parentid2" })
}
</div>
如果下拉列表没有任何值,则不允许保存。
我不想从Model
验证答案 0 :(得分:0)
如果要在视图中使用java脚本,则可以使用以下验证.js的Java脚本。
$("#frmId").validate({
rules: {
myFieldName: {
required: 'true'// <- declare the rule
}
},
messages: {
myFieldName: {
required: "myFieldName is required",
},
}
});
}