KingKerosin
KingKerosin

Reputation: 3841

MVC DropDownListFor - SelectedItem not working for nested value

In my C#, MVC4 .NET WebApplication I have a custom helper that has the following method-call:

@Custom.DropDownListFor(m => m.CategorySetups[categoryId].Gender, CategoryHelper.Genders, new { @class = "form-control" })

which is internally something like:

@Html.DropDownListFor(expression, new SelectList(items, "Value", "Text"))

because I have an override of SelectListItem here.

This call is not selecting the value in the rendered view as stored in

m.CategorySetups[categoryId].Gender

If I call the method this way (not in the same view):

@Custom.DropDownListFor(m => m.Gender, CategoryHelper.Genders, new { @class = "form-control" })

The rendered HTML has the correct value marked as

<option value="Under15" selected>Under15</option>

So my guess is, that is has something to do with nesting of the property that is selected but I don't see it.

From debugging I can see, that

m.CategorySetups[categoryId].Gender

has the correct value set when it should render the select.

Note: Not sure if this may causing the issue (because the second way it's working) but the bound values Type is Enum.

Update as of comments:

The implementation is as follows:

 public static readonly List<SelectListItem<ClassificationType>> ClassificationTypes = new List<SelectListItem<ClassificationType>>
        {
            new SelectListItem<ClassificationType> {Text = @"please select...", Value = ClassificationType.NotSet},
            new SelectListItem<ClassificationType> {Text = EnumParser.GetString(ClassificationType.Under13), Value = ClassificationType.Under13},
            new SelectListItem<ClassificationType> {Text = EnumParser.GetString(ClassificationType.Under15), Value = ClassificationType.Under15},
            new SelectListItem<ClassificationType> {Text = EnumParser.GetString(ClassificationType.Under18), Value = ClassificationType.Under18},
            new SelectListItem<ClassificationType> {Text = EnumParser.GetString(ClassificationType.Over30), Value = ClassificationType.Over30},
            new SelectListItem<ClassificationType> {Text = EnumParser.GetString(ClassificationType.Open), Value = ClassificationType.Open}
        };

Upvotes: 0

Views: 1822

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

The SelectList contructor has an overload which takes a parameter object selectedValue, you need to specifiy the selected value, In normal DropDownListFor() we do this way:

@Html.DropDownListFor(m=>m.Gender,
                     new SelectList(Model.Genders,"Id","Value",Model.Gender),
                     new { @class = "form-control" })

UPDATED:

In your case you can do this:

@Custom.DropDownListFor(m => m.CategorySetups[categoryId].Gender, 
                        new SelectList(CategoryHelper.Genders,"Value","Text",Model.CategorySetups[categoryId].Gender), 
                        new { @class = "form-control" })

One thing more, as you are populating items via Enum, you can use extension method I posted in this answer

Upvotes: 1

Related Questions