0

I have an enum, which is a mapping for a description of a property against an index in the database. I have a property on my ViewModel that represents an instance of that enum. I've tried both returning a list of enum instances, which means I do this:

@Html.DropDownListFor(m => m.CurrentFilter,
                    Model.FilterTypes.Select(entry =>
                        new SelectListItem{ Text = entry.ToString(), Value = ((int)entry).ToString()}),
                    new { @class = "normalcell", style = "WIDTH: 132px;" })     

and returning a list of SelectListItems, which means I do this:

            @Html.DropDownListFor(m => m.CurrentFilter,
                    Model.FilterTypes.Select(entry =>
                        new SelectListItem{ Text = entry.Text, Value = entry.Value, Selected = entry.Selected}),
                    new { @class = "normalcell", style = "WIDTH: 132px;" })                         

In the second case, when I debug, I am certain that the Selected property on the entry object is true for the correct item. In both cases, there is no 'selected' attribute written in to my HTML and so the correct item is not selected. I've also set a breakpoint, and CurrentFilter DOES have the correct value and the rest of my page renders appropriately, so it's finding the value.

I've written plenty of drop lists that work, using similar code, I can't for the life of me see why this does not work, no matter how I try to do it ?

I have also tried:

@Html.DropDownListFor(m => m.CurrentFilter,
                    Model.FilterTypes,
                    new { @class = "normalcell", style = "WIDTH: 132px;" })     

which seems to me to be the logical way to do it ( return a list of SelectListItems and just do no processing in the page ), but the Selected property is still ignored.

Update:

I tried to do it this way:

@Html.DropDownList("CurrentFilter", Model.FilterTypes, new { @class = "normalcell", style = "WIDTH: 132px;" })      

and just read the value out of the request. It's still the case that I am returning a list with only one item that has Selected == true, and it's still the case that MVC is ignoring it.

This works, not surprisingly, but I'd love to know why all the other things don't.

<select class="normalcell" id="CurrentFilter" name="CurrentFilter" style="WIDTH: 132px;">
  @foreach (SelectListItem item in Model.FilterTypes)
  {
      if (item.Selected)
      {
           <option value="@item.Value" selected="selected">@item.Text</option>
      }
      else
      {
         <option value="@item.Value">@item.Text</option>
      }
   }

cgraus
  • 784
  • 2
  • 9
  • 26
  • regarding your working code you could rewrite it as `@(item.Selected ? "selected='selected'" : "")` – Baz1nga Jun 22 '12 at 06:10
  • Thanks - you are right, but this was always a place holder until I work out how to make it work with 'proper' MVC code – cgraus Jun 22 '12 at 10:01

2 Answers2

0

I believe the reason is that the MVC binding engine doesn't know how to deal with Enum values. I think you need to create a "proxy" property for your view model. Something like this...

public enum MyEnum { a, b, c, d };

public MyEnum EnumVal { get; private set; }

public string EnumProxy
{
    get { return EnumVal.ToString(); }
    set { EnumVal = (MyEnum)Enum.Parse(typeof(MyEnum), value); }
}

Then construct the drop-down list using the Enum names:

Type t = typeof(MyEnum);
var ddList = Enum.GetNames(t).Select(
    item => new SelectListItem() { Text = item, Value = item }
).ToArray();

Now you should be able to use DropDownListFor normally:

@Html.DropDownListFor(model => model.EnumProxy, ddList)

I'm not sure if there's a neater solution. This one should work though.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • Thanks - I will try that, I figured the issue could be to do with enums, but I hoped as my enum has explicit int values, that it would just automagically deal with them as ints. – cgraus Jun 22 '12 at 10:01
0

My DDL tutorial shows how to use enums. See See my tutorial Working with the DropDownList Box and jQuery and My blog Cascading DropDownList in ASP.Net MVC Part 2 of the tutorial explains: When the string argument (the property to bind) and the SelectList object have the same name, the selected value is not used.

Darin has a SO post on another common reason the selected value is not display. See MVC DropDownList SelectedValue not displaying correctly

Community
  • 1
  • 1
RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78