This is a migrated thread and some comments may be shown as answers.

DropDownListFor Makes Property Required?

1 Answer 874 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 19 Sep 2013, 10:04 PM
Hi y'all,

I'm having a strange issue with the DropDownListFor functionality.  Whenever I use it on a property in my model, it is attaching data-val-required on the resultant html tag, even though the property is not required.  This is posing a problem, because I want to use the optionLabel as a way of letting the user select a null value for that field.  I double-checked to make sure it was just Kendo's control that was doing this by using the built-in Html.TextBoxFor with the same property, and it did not have the data-val-required put on the text input.  Any help?

Here's the Model:
public class ComponentGIEditViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
 
        [Display(Name = "Toolset Reference Id")]
        public string ToolsetReferenceId { get; set; }
 
        [Display(Name = "BU Reference Id")]
        public string BUReferenceId { get; set; }
 
        [Display(Name = "Service Group")]
        public int ServiceGroupId { get; set; }
        public IEnumerable<dynamic> ServiceGroups { get; set; }
 
        [Display(Name="Component Type")]
        public int ComponentTypeId { get; set; }
        public IEnumerable<dynamic> ComponentTypes { get; set; }
 
        [Display(Name = "Criticality")]
        public int CriticalityId { get; set; }
        public IEnumerable<dynamic> Criticalities { get; set; }
 
        [Display(Name="Is Sox")]
        public string IsSOX { get; set; }
 
        [Display(Name = "Complexity")]
        public int ComplexityId { get; set; }
        public IEnumerable<dynamic> Complexities { get; set; }
 
        [Display(Name = "Trigger")]
        public int TriggerId { get; set; }
        public IEnumerable<dynamic> Triggers { get; set; }
 
        [Display(Name = "Data Classification")]
        public int DataClassificationId { get; set; }
        public IEnumerable<dynamic> DataClassifications { get; set; }
    }

Here's the relevant part of the View:

@Html.LabelFor(model => model.ServiceGroupId, new { @class = "col-md-1 control-label" })
            <div class="col-md-5">
                @(Html.Kendo().DropDownListFor(model => model.ServiceGroupId)
                .BindTo(Model.ServiceGroups)
                .DataTextField("Name")
                .DataValueField("Id")
                .OptionLabel("-- None --")
                .HtmlAttributes(new { style = "width: 100%" })
            )
            </div>


Here's the resultant HTML:

                <span tabindex="0" class="k-widget k-dropdown k-header" role="listbox" aria-busy="false" aria-disabled="false" aria-expanded="false" aria-haspopup="true" aria-readonly="false" aria-activedescendant="ServiceGroupId_option_selected" aria-owns="ServiceGroupId_listbox" unselectable="on">
<
span class="k-dropdown-wrap k-state-default" unselectable="on">
<
span class="k-input" unselectable="on">Telecommunications</span>
<
span class="k-select" unselectable="on">
<
span class="k-icon k-i-arrow-s" unselectable="on">select</span></span></span>
<
input name="ServiceGroupId" id="ServiceGroupId" style="display: none;" type="text" value="437" data-val-required="The Service Group field is required." data-val-number="The field Service Group must be a number." data-val="true" data-role="dropdownlist"></span>
<script>
    jQuery(function(){jQuery("#ServiceGroupId").kendoDropDownList({"dataSource":[{"Id":437,"Name":"Telecommunications"},{"Id":438,"Name":"UGIS - Alaska - Technical/Business"},{"Id":439,"Name":"UGIS - AP - Technical - Australia"},{"Id":440,"Name":"UGIS - Canada - Business"},{"Id":441,"Name":"UGIS - Canada - Technical"},{"Id":442,"Name":"UGIS - Europe \u0026 W Africa - Technical - Norway"},{"Id":443,"Name":"UGIS - L48 - TIS"},{"Id":394,"Name":"CGIS - Risk Management - Price Feeds"},{"Id":395,"Name":"Common Operating Environment (Client and Server)"},{"Id":396,"Name":"Corporate Records Management - Core"},{"Id":397,"Name":"CSGIS - Corporate Staffs GIS Admin"},{"Id":398,"Name":"CSGIS - GFS - Treasury Services"},{"Id":399,"Name":"CSGIS - GRFS"},{"Id":400,"Name":"CSGIS - HR - Medical"},{"Id":401,"Name":"CSGIS - HR - NA Leveraged Service Center"},{"Id":402,"Name":"CSGIS - HSE - Admin"},{"Id":403,"Name":"CSGIS - HSE - Risk Mgmt"},{"Id":404,"Name":"CSGIS - Legal"},{"Id":405,"Name":"CSGIS - Political Action Committee"},{"Id":406,"Name":"CSGIS - T\u0026MP - Global Procurement Services"},{"Id":407,"Name":"CSGIS - Tax"},{"Id":408,"Name":"DGIS - Germany"},{"Id":409,"Name":"DGIS - Mkt Leveraged Svc Retail Svc - JetLink"},{"Id":410,"Name":"DGIS - Mkt Leveraged Svc Retail Svc - LOPS"},{"Id":411,"Name":"DGIS - Mkt Leveraged Svc Retail Svc - R\u0026M"},{"Id":412,"Name":"DGIS - Mkt Leveraged Svc Retail Svc - RSI"},{"Id":413,"Name":"DGIS - Mkt Leveraged Svc Retail Svc - SKPI"},{"Id":414,"Name":"DGIS - Mkt Leveraged Svc S\u0026T Svc - iCAST"},{"Id":415,"Name":"DGIS - Mkt Leveraged Svc S\u0026T Svc - Medata"},{"Id":416,"Name":"DGIS - Mkt Leveraged Svc S\u0026T Svc - TAS"},{"Id":417,"Name":"DGIS - Retail Marketing"},{"Id":418,"Name":"DGIS - Specialties - Lubes Finished"},{"Id":419,"Name":"DGIS - Specialties - Solvents"},{"Id":420,"Name":"DGIS - US Refining \u0026 Tehnology - Leveraged"},{"Id":421,"Name":"DGIS - US Refining Direct - Bayway"},{"Id":422,"Name":"DGIS - US Refining Direct - LA"},{"Id":423,"Name":"DGIS - US Refining Direct - Lake Charles"},{"Id":424,"Name":"DGIS - US Refining Direct - LC"},{"Id":425,"Name":"DGIS - US Refining Direct - Rodeo"},{"Id":426,"Name":"DGIS - US Refining Direct - WoodRiver"},{"Id":427,"Name":"DGIS - Warwick"},{"Id":428,"Name":"DGIS - Wholesale Marketing"},{"Id":429,"Name":"DGIS -Transportation - Marine"},{"Id":430,"Name":"DGIS -Transportation - PL\u0026TRM"},{"Id":431,"Name":"DGIS -Transportation - Trk\u0026RL"},{"Id":432,"Name":"Financial Consolidation Service"},{"Id":433,"Name":"Global SAP - Production Support - Core Modules"},{"Id":434,"Name":"Land Information Services (Tobin)"},{"Id":435,"Name":"Performance Services"},{"Id":436,"Name":"Prod, Rev, Reg \u0026 Accting Solutions (Novistar)"},{"Id":390,"Name":"CGIS - Crude \u0026 Products - Americas Crude Systems"},{"Id":391,"Name":"CGIS - Crude \u0026 Products - STAR"},{"Id":392,"Name":"CGIS - Gas \u0026 Power"},{"Id":393,"Name":"CGIS - Risk Management - Non-Price Feeds"}],"dataTextField":"Name","autoBind":false,"dataValueField":"Id","optionLabel":"-- None --"});});
</script>

            

1 Answer, 1 is accepted

Sort by
1
Georgi Krustev
Telerik team
answered on 20 Sep 2013, 10:22 AM
Hello Mike,

The unobtrusive data annotation will render data-val-required, because the property is non-nullable. This is built-in feature of the ASP.NET MVC validation. Check this screencast, which shows that. You can fix this, if you make the field nullable.

Regards,
Georgi Krustev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
DropDownList
Asked by
Mike
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or