當我在MVC Razor視圖中使用Kendo UI組合框和DropDownList控件時,客戶端驗證不會觸發。這裏有一個例子:如何爲Kendo UI DropDownList和ComboBox啓用ASP.Net MVC客戶端驗證?
@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"
},
};
}
非劍道UI下拉適當顯示驗證錯誤時提交表單,但劍道控件不。請讓我知道是否有辦法爲這些控件啓用客戶端驗證,而無需手動連線。
完整的例子液附着於以下劍道論壇上發帖: http://www.kendoui.com/forums/mvc/dropdownlist/mvc-client-validation-not-working.aspx
對於jQuery驗證1.9,你必須[組'ignore'爲空數組(http://stackoverflow.com/a/8565769/26226)。 – jrummell
發現了一個很好的補充。如果您有其他控件,如果隱藏時不想驗證其他控件,但希望隱藏特定控件,則可以將它們添加到不(忽略)以展開此有用提示:例如,我們將.kendoDD標記在Kendo Dropdown上,然後設置: $ .validator.setDefaults({0:隱藏:不((.kendoDD)「 }); https://stackoverflow.com/questions/20287567/ignore-all-hidden-div-but-not-one-in-jquery-validation – Mike