Anastasios Selmani
Anastasios Selmani

Reputation: 3689

Kendo validation message in inLine Edit mode kendo grid not working through Data annotation

I am using the kendo UI for ASP.NET MVC. I have an inline Edit mode kendo grid with some ClientEditorTemplate DropDownLists in my columns. The grid is binded to a list of models "ContractPricingPolicy".

public class ContractPricingPolicy
{
    //Some other fields

    [CustomDisplayName(XmlResourceNames.ContractPricingPoliciesXml, "PricingPolicyType")]
    [UIHint("ClientPricingPolicyType")]
    [Required(ErrorMessage = "Tο πεδίο {0} είναι υποχρεωτικό")]
    public PricingPolicyType PricingPolicyType { get; set; }

    //Some other fields
}

As you can see I use the UIHint for the EditorTemplate and I use the Required Data Annotation for the validation of my field. The editor Template is like that:

@(Html.AthenaTransportDdl("GetPricingPolicyTypes", "Enterprise", HttpVerbs.Post)
    .Name("PricingPolicyType")
    .DataTextField("Label")
    .DataValueField("Id")
    .AutoBind(false)
    .Filter("contains")
    .HtmlAttributes(new { @class = "atn-flexdisplay-container" })
    .OptionLabel(Html.GetResource(AthenaWeb.Models.Common.XmlResourceNames.CommonXml, "PricingPolicyTypeOptionLabel"))
)

My kendo grid is like that:

@(Html.Kendo().Grid(Model.PricingPolicies)
        .Name("pricingPoliciesGrid")
        .Columns(columns =>
        {
            columns.Bound(c => c.Id).Hidden();
            columns.Bound(c => c.PricingPolicyType).Width(100)
            // Some more columns
            columns.Command(command => { command.Edit().Text(" ").UpdateText(" ").CancelText(" "); command.Destroy().Text(" "); }).Width(70)
            .HtmlAttributes(new { style = "text-align:center" });
        })
        .ToolBar(toolbar => toolbar.Template("<div class='col-md-2'><button id='addPPRowBtn' class='btn btn-success' data-atn-mouseover='cStatusDisabled'><span class='fa fa-plus'></span> " + Html.GetResource(gcw, "ToolBarInsertNew") + "</div>"))
        .Editable(editable => editable.Mode(GridEditMode.InLine).DisplayDeleteConfirmation(Html.GetResource(cpp, "DeleteConfirmationMsg")))
        .DataSource(dataSource => dataSource
            .Ajax()
            .ServerOperation(true)
            .Model(model =>
            {
                model.Id(c => c.Id);
                model.Field(p => p.PricingPolicyType).DefaultValue(new AthenaWeb.Models.Enterprise.PricingPolicyType());
                // Some other fields
            })
        .Create(create => create.Action("PricingPolicy_Create", "Student"))
        .Read(read => read.Action("PricingPolicy_Read", "Student", new { id = Model.Id }))
        .Destroy(update => update.Action("PricingPolicy_Deactivate", "Student"))
        )
    )

When I insert a new row and try to save my row without having selected a pricing policy Type the validation from the Data Annotation doesn't trigger. However if I set the attribute required to my Editor Template like that:

(Html.AthenaTransportDdl("GetPricingPolicyTypes", "Enterprise", HttpVerbs.Post)
    .Name("PricingPolicyType")
    .DataTextField("Label")
    .DataValueField("Id")
    .AutoBind(false)
    .Filter("contains")
    .HtmlAttributes(new { @class = "atn-flexdisplay-container", required = "required" })
    .OptionLabel(Html.GetResource(AthenaWeb.Models.Common.XmlResourceNames.CommonXml, "PricingPolicyTypeOptionLabel"))
)

the validation triggers but the message displayed is the default The field {NameOfField} is Required while I need to show the message in my Data Annotation.

My question is why is this happening and how could I enable the Data Annotation validation message to be displayed (I am guessing the exact question is how to trigger the kendoValidator for the row for a field binded to an Editor Template)

Upvotes: 2

Views: 1928

Answers (1)

bharathy
bharathy

Reputation: 162

For the validations to work for the columns using Editor Templates you need to add the following in your Editor Templates

.HtmlAttributes(Html.GetUnobtrusiveValidationAttributes("Validation",
ViewData.ModelMetadata))

    (Html.AthenaTransportDdl("GetPricingPolicyTypes", "Enterprise", HttpVerbs.Post)
        .Name("PricingPolicyType")
        .DataTextField("Label")
        .DataValueField("Id")
        .AutoBind(false)
        .Filter("contains")
        .HtmlAttributes(Html.GetUnobtrusiveValidationAttributes("Validation",     
           ViewData.ModelMetadata))  
.OptionLabel(Html.GetResource(AthenaWeb.Models.Common.XmlResourceNames.CommonXml, "PricingPolicyTypeOptionLabel"))
    )

Upvotes: 1

Related Questions