[RESOLVED]Custom dropdown validation issue


I am new to MVC. i have created custom dropdown list control.

am able to bind the data to that control.

but Validation is not working when click on submit button.

My Custom Class

 public static MvcHtmlString CustomDropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList)
            var sb = new StringBuilder();

            var select = new TagBuilder("select");
            select.Attributes.Add("name", "ddl_" + name);
            select.Attributes.Add("data-val", "true");
            select.Attributes.Add("data-val-required", "This field is required.");
            var options = "";
            TagBuilder option;
            option = new TagBuilder("option");
            option.MergeAttribute("value", "-1");
            options += option.ToString(TagRenderMode.Normal) + "n";
            foreach (var item in selectList)
                option = new TagBuilder("option");
                option.MergeAttribute("value", item.Value.ToString());
              //  option.MergeAttribute("data-domain", item.Value.ToString());
                //option.Attributes.Add("name", "ddl_" + name);
                options += option.ToString(TagRenderMode.Normal) + "n";
            select.InnerHtml = options;

            return new MvcHtmlString(select.ToString(TagRenderMode.Normal));


UICode(After rendered in browser)

<select data-val="true" data-val-required="This field is required." name="ddl_0">
<option value="-1">--Select--</option>
<option value="0">Item 0</option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<span class="field-validation-valid" data-valmsg-for="ddl_0" data-valmsg-replace="true"></span>

Please help me on this.




Try to include all validation related jquery files and the test

It’s working fine. Your select always has a value. Change the -1 value to blank, or create your own validator that gives an error when the value is -1

Hi bruce,

Thanks for your reply.Its working fine without "-1" value.

May i know the reason why its not working when we have the value "-1" on select?



Leave a Reply