当我在 MVC Razor 视图中使用 Kendo UI ComboBox 和 DropDownList 控件时,客户端验证不会触发。这是一个例子:
@using Kendo.Mvc.UI
@model KendoDropDownTest.Models.TestModel
@{
ViewBag.Title = "Kendo Drop Down and Combo Box Test";
}
<h2>Kendo Drop Down and Combo Box Test</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<div>
@Html.LabelFor(x => x.DropDownValue)
@(Html.DropDownListFor(x => x.DropDownValue, Model.Options, "-- Select an Option --"))
@Html.ValidationMessageFor(x => x.DropDownValue)
</div>
<fieldset>
<legend>Kendo</legend>
<div>
@Html.LabelFor(x => x.KendoComboValue)
@(Html.Kendo().ComboBoxFor(x => x.KendoComboValue)
.BindTo(Model.Options.Select(x => x.Text)))
@Html.ValidationMessageFor(x => x.KendoComboValue)
</div>
<div>
@Html.LabelFor(x => x.KendoDropDownValue)
@(Html.Kendo().DropDownListFor(x => x.KendoDropDownValue)
.OptionLabel("-- Select an Option --")
.BindTo(Model.Options))
@Html.ValidationMessageFor(x => x.KendoDropDownValue)
</div>
</fieldset>
<input type="submit" value="Submit" />
}
以及对应的型号:
public class TestModel
{
[Required]
public string DropDownValue { get; set; }
[Required]
public string KendoComboValue { get; set; }
[Required]
public string KendoDropDownValue { get; set; }
public SelectListItem[] Options = new[]
{
new SelectListItem
{
Text = "Option 1",
Value = "1"
},
new SelectListItem
{
Text = "Option 2",
Value = "2"
},
new SelectListItem
{
Text = "Option 3",
Value = "3"
},
};
}
提交表单时,非 Kendo UI 下拉菜单会适当地显示验证错误,但 Kendo 控件不会。请让我知道是否有一种方法可以为这些控件启用客户端验证,而无需手动连接它。
以下剑道论坛帖子附有完整的示例解决方案: http ://www.kendoui.com/forums/mvc/dropdownlist/mvc-client-validation-not-working.aspx