Jas*_*ale 6 asp.net-mvc jquery-validate unobtrusive-validation kendo-ui
当我在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" />
}
Run Code Online (Sandbox Code Playgroud)
和相应的型号:
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"
},
};
}
Run Code Online (Sandbox Code Playgroud)
非Kendo UI下拉菜单适当地显示提交表单时的验证错误,但Kendo控件没有.如果有办法为这些控件启用客户端验证,请告诉我,而无需手动连接它.
完整的示例解决方案附加到以下Kendo论坛帖子:http: //www.kendoui.com/forums/mvc/dropdownlist/mvc-client-validation-not-working.aspx
Jas*_*ale 12
基于对Kendo论坛的响应,验证不起作用的原因是因为jquery validate默认情况下不验证隐藏字段.更改它的最简单方法是使用$ .validate.setDefaults方法来覆盖这种行为,如下所示:
$.validator.setDefaults({
ignore: ""
});
Run Code Online (Sandbox Code Playgroud)
这仍然不会将"input-validation-error"类添加到组合框或下拉列表中,但至少会添加验证错误,并使表单不会被提交.
| 归档时间: |
|
| 查看次数: |
13197 次 |
| 最近记录: |