我有 Kendo DropDownList 与模型绑定(bind)。现在我已经在模型中应用了所需的验证,但它没有触发。我不知道是什么问题。 型号:
[Required(ErrorMessage = "Gender is required.")]
public string Gender { get; set; }
查看页面:
@(Html.Kendo().DropDownListFor(m=>m.Gender)
.DataTextField("Text")
.DataValueField("Value")
.OptionLabel("---Select---")
.HtmlAttributes(new { style = "width:100%" })
.AutoBind(true)
.BindTo(new List<SelectListItem>() {
new SelectListItem() { Text = "Male", Value = "Male" },
new SelectListItem() { Text = "Female", Value = "Female" }
})
)
@Html.ValidationMessageFor(m => m.Gender, null, new { style = "color:red;float:left" })
对于其他模型属性,它会触发验证。
最佳答案
如果您使用的是 jquery.validate 1.9+,那么默认情况下它会忽略隐藏的输入,并且下拉列表使用一个来保存值。为了在这种情况下启用验证,您应该覆盖验证器的默认设置,例如
$.validator.setDefaults({ ignore: '' });
详细内容可以引用链接 click here
关于jquery - Kendo DropDownList 未触发验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24879656/