0

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}
        };
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
KingKerosin
  • 3,639
  • 4
  • 38
  • 77

1 Answers1

1

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

Community
  • 1
  • 1
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Any simple way to get the value from the expression? Otherwise I have to change the call(s) to @Custom.DropDownListFor(m => m.Gender, CategoryHelper.Genders, new { @class = "form-control" }) where I don't have the selectedValue parameter but the expression to the property – KingKerosin Mar 04 '15 at 12:23
  • @KingKerosin you don't want to do with the second way? – Ehsan Sajjad Mar 04 '15 at 12:28
  • I misread the answer... I made slight changes and it works now as expected. Thanks. Accepted! – KingKerosin Mar 04 '15 at 12:42