Grid custom editor dropdown - data annotation

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

Hi,

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)
    .Name("CountryCode")
    .Value(Model)
    .SelectedIndex(0)
    //.DataValueField("CountryCode")
    //.DataTextField("CountryCode")  
    .BindTo(new string[] { "GB", "CN" })
    .HtmlAttributes(new { style = "font-size: 12px;" , required = "required", data_bind = "value: CountryCode" })
)

 

EDIT:

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


<script id="AppRegGrid" type="text/kendo-tmpl">
    @(Html.Kendo().Grid<Myapp.Models.AppReg>()
                .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
                    .Ajax()
                    .Model (m => {
                        m.Id(l => l.AppRegNumID);
                        m.Field(l => l.AppRegNumID).Editable(false);
                    })
                    //.Sort(s => s.Add("ERPShipmentNumber").Ascending())
                    .PageSize(10)
                    .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#" }))
                    .ServerOperation(true)
                )
                .HtmlAttributes(new { style = "font-size: 12px;" })
                .Height(200)
                .ToolBar(tools => tools.Create())
                .ToClientTemplate()
    )
</script>

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")]
        [StringLength(2)]
        [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "2 letter ISO code only.")]
        [UIHint("AppRegistrationCountryCode")]
        [Required]
        public string CountryCode { get; set; }

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

    }

 

No answers yet. Maybe you can help?

Tags
DropDownList Grid
Asked by
Jonathan
Top achievements
Rank 1
Share this question
or