为什么不在编辑字段的编辑器模板上向我显示网格验证? 我不明白为什么不阅读数据注释。 抱歉英语不好......
起初我创建了这样的剑道网格:
<div id="grid">
@(Html.Kendo().Grid<CardView>()
.Name("Grid")
.Columns(x =>
{
x.Bound(c => c.CardID).Title("Card Nm.");
x.Bound(c => c.ExpirationDate).Format("{0:dd/MM/yyyy}");//.EditorTemplateName("KendoDatePicker");
x.Command(cmd =>
{
cmd.Edit();
}).Title("Edit");
})
.BindTo(Model)
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Id(x => x.CardID);
})
.ServerOperation(true)
.Read(read => read.Action("Index", "Home"))
.Events(events => events.Error("error_handler"))
.Update(update => update.Action("Update", "Home"))
.Editable(editable =>editable.Mode(GridEditMode.InLine)))
</div>
这是在数据源事件错误中使用的javascript:
<script>
function error_handler(e, status) {//Klaidu isvedimas
if (e.errors) {
var message = "Error:\n";
var grid = $('#GrdKendo').data('kendoGrid');
var gridElement = grid.editable.element;
var validationMessageTemplate = kendo.template(
"<div id='#=field#_validationMessage' " +
"class='k-widget k-tooltip k-tooltip-validation " +
"k-invalid-msg field-validation-error' " +
"style='margin: 0.5em;' data-for='#=field#' " +
"data-val-msg-for='#=field#' role='alert'>" +
"<span class='k-icon k-warning'></span>" +
"#=message#" +
"<div class='k-callout k-callout-n'></div>" +
"</div>");
$.each(e.errors, function (key, value) {
if (value.errors) {
gridElement.find("[data-valmsg-for=" + key + "],[data-val-msg-for=" + key + "]")
.replaceWith(validationMessageTemplate({ field: key, message: value.errors[0] }));
gridElement.find("input[name=" + key + "]").focus();
}
});
grid.one("dataBinding", function (e) {
e.preventDefault(); // cancel grid rebind
});
}
}
当我创建 viewModel 时(对CardID工作进行验证,但不使用编辑器模板处理expirationDate):
public class CardView
{
[Required(ErrorMessage = "Card Expiration Date")]
public virtual string CardID { get; set; }
[UIHint("DatePicker")]
[Required(ErrorMessage = "Card Expiration Date")]
public virtual DateTime ExpirationDate { get; set; }
}
我在 Views \ Shared \ EditorTemplates **位置创建了编辑器模板,名称为** DatePicker.cshtml :
@model DateTime?
@(Html.Kendo()
.DatePicker()
.Name(ViewData.ModelMetadata.PropertyName.ToString())
.Format("{0:dd/MM/yyyy}"))
那么如何在编辑器模板字段中读取数据注释? 数据annotacions在不使用编辑器模板
的字段上工作得很好答案 0 :(得分:4)
我找到了解决方案(搜索了大约两天)。需要在编辑器模板的html属性中添加Html.GetUnobtrusiveValidationAttributes(&#34; Validation&#34;,ViewData.ModelMetadata)
这是代码:
@model DateTime?
@(Html.Kendo()
.DatePicker()
.Name(ViewData.ModelMetadata.PropertyName.ToString())
.Format("{0:dd/MM/yyyy}")
.HtmlAttributes(Html.GetUnobtrusiveValidationAttributes("Validation",
ViewData.ModelMetadata))
)