0

Why not show grid validation to me on editor template on editing field? I don't understand why not reading data annotation. Sorry for bad english language...

At first I created kendo grid like this:

 <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> 

Here is javascript which is using in datasource event error:

<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 }); } } 

When I created my viewModel(validation on CardID working, but dont working on expirationDate which using editor template):

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; } } 

And I created editor template in Views\Shared\EditorTemplates** location with name **DatePicker.cshtml :

 @model DateTime? @(Html.Kendo() .DatePicker() .Name(ViewData.ModelMetadata.PropertyName.ToString()) .Format("{0:dd/MM/yyyy}")) 

So how to read data annotacion on editor template field? Data annotacions works perfect on field which is not using editor template

1 Answer 1

5

I found solution(searched about two days). Need add Html.GetUnobtrusiveValidationAttributes("Validation", ViewData.ModelMetadata) in html attributes in editor template

Here is code:

@model DateTime? @(Html.Kendo() .DatePicker() .Name(ViewData.ModelMetadata.PropertyName.ToString()) .Format("{0:dd/MM/yyyy}") .HtmlAttributes(Html.GetUnobtrusiveValidationAttributes("Validation", ViewData.ModelMetadata)) ) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering this. You should mark this as the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.