use*_*571 0 c# asp.net-mvc razor kendo-ui kendo-grid
为什么不在编辑字段的编辑器模板上向我显示网格验证?我不明白为什么不阅读数据注释。抱歉英语不好...
起初我像这样创建了剑道网格:
<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>
Run Code Online (Sandbox Code Playgroud)
这是在数据源事件错误中使用的 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
});
}
}
Run Code Online (Sandbox Code Playgroud)
当我创建我的viewModel 时(CardID验证工作,但不使用编辑器模板的expireDate工作):
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; }
}
Run Code Online (Sandbox Code Playgroud)
我在Views\Shared\EditorTemplates** 位置创建了编辑器模板,名称为 **DatePicker.cshtml:
@model DateTime?
@(Html.Kendo()
.DatePicker()
.Name(ViewData.ModelMetadata.PropertyName.ToString())
.Format("{0:dd/MM/yyyy}"))
Run Code Online (Sandbox Code Playgroud)
那么如何读取编辑器模板字段上的数据注释呢?数据注释在不使用编辑器模板的字段上工作完美
我找到了解决方案(搜索了大约两天)。需要在编辑器模板的 html 属性中添加 Html.GetUnobtrusiveValidationAttributes("Validation", ViewData.ModelMetadata)
这是代码:
@model DateTime?
@(Html.Kendo()
.DatePicker()
.Name(ViewData.ModelMetadata.PropertyName.ToString())
.Format("{0:dd/MM/yyyy}")
.HtmlAttributes(Html.GetUnobtrusiveValidationAttributes("Validation",
ViewData.ModelMetadata))
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1594 次 |
| 最近记录: |