Grid custom editor dropdown - data annotation

0 Answers 75 Views
DropDownList Grid
Top achievements
Rank 1
Jonathan asked on 24 Mar 2023, 01:08 PM | edited on 24 Mar 2023, 01:14 PM


I have created a new custom view to enforce values in a grid dropdown.

This works well, however it is not picking up data annotations defined in my model. Initially it didn't even trigger the validation messages, however I've managed to fix this by added "required = "required" in the HTMLATTRIBUTES.  I can't seem to find a way to add data-val-required property here.

Is there a way to make this work? I have seen an example here on custom editors but this seems to complicated for what I need (enforcing static values in the grid dropdown).

Hopefully someone can kindly point me in the right direction.


@model string

@(Html.Kendo().DropDownListFor(m => m)
    .BindTo(new string[] { "GB", "CN" })
    .HtmlAttributes(new { style = "font-size: 12px;" , required = "required", data_bind = "value: CountryCode" })



This is the code for the grid, and how I can referencing this custom dropdown list:

<script id="AppRegGrid" type="text/kendo-tmpl">
                .Name("AppReg_#=id#") // template expression, to be evaluated in the master context
                .Columns(columns =>
                    columns.Bound(c => c.Number).Title("Registration Number").Width(120);
                    columns.Bound(c => c.CountryCode).Title("Country Code").Width(50).EditorTemplateName("AppRegistrationCountryCode");
                    columns.Bound(c => c.TypeCode).Title("Type Code").Width(50).EditorTemplateName("AppRegistrationTypeCode");
                    columns.Command(command => { command.Edit();  }).Width(200).HtmlAttributes(new { style = "text-align: center" }).HeaderHtmlAttributes(new { style = "text-align: center" });
                .DataSource(dataSource => dataSource
                    .Model (m => {
                        m.Id(l => l.AppRegNumID);
                        m.Field(l => l.AppRegNumID).Editable(false);
                    //.Sort(s => s.Add("ERPShipmentNumber").Ascending())
                    .Read(read => read.Action("AppRegistrationNumbers_Read", "Admin", new { id = "#=id#" }))
                    .Update(update => update.Action("AppRegistrationNumbers_Update", "Admin"))
                    .Create(create => create.Action("AppRegistrationNumbers_Create", "Admin", new { id = "#=id#" }))
                .HtmlAttributes(new { style = "font-size: 12px;" })
                .ToolBar(tools => tools.Create())

And this is by model:

    public class AppReg

        public int AppRegNumID { get; set; }
        [DisplayName("Registration Number")]
        [Required(ErrorMessage = "Please enter a valid registration number.")]
        public string Number { get; set; }

        [DisplayName("Country Code")]
        [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "2 letter ISO code only.")]
        public string CountryCode { get; set; }

        [DisplayName("Type Code")]
        public string TypeCode { get; set; }



No answers yet. Maybe you can help?

DropDownList Grid
Asked by
Top achievements
Rank 1
Share this question