2

Fairy routine situation, I need to allow users to select a value from an Enum in a ASP.NET MVC application. Since I'm using MVC version 5.2, I can simply do this:

// Model
public MyEnum EnumVal { get; set; }

// Controller
return View(new MyModel{EnumVal = MyEnum.SomeVal});

// View
@Html.EnumDropDownListFor(model => model.EnumVal)

And it all just magically works. I've done it enough times to take it for granted.

But I've just noticed that the value I'm passing isn't being selected as the current value of the drop down.

I can spit out the contents of the model to verify that the value is being passed correctly, but the drop down only ever has the first value selected.

Other answers (e.g. here and here) seem to confirm what I thought I knew: that I shouldn't need to do anything additional to get the drop down to select the value I've passed.

Am I having a brain fart? Why isn't this working?


Update

Here's a more complete listing for the controller method:

public ActionResult Create(string enumVal)
{
    var valueToPass = AccountsDestination.QbDUk;
    Enum.TryParse(enumVal, out valueToPass);
    return View(new QuickBooksMappingViewModel(){EnumVal = valueToPass});
}
Community
  • 1
  • 1
Tom Wright
  • 11,278
  • 15
  • 74
  • 148
  • The code you have shown works fine. Is there any javascript which may be causing an issue? And I assume that controller method is a GET and your not trying to change the value in a POST, or that you do not have a parameter for the `enum` in your GET method. –  Apr 11 '17 at 09:25
  • @StephenMuecke You nailed it! It was a clash with the name of one of the get params. Renaming the parameter was all it took. (Feel free to upgrade this to an answer and I'll accept it.) – Tom Wright Apr 11 '17 at 09:28
  • You have not shown the relevant code - the signature of the method :) –  Apr 11 '17 at 09:29
  • Good point. See my update. Interesting side point: my code anonymisation may well have stripped out the relevant variable names anyway, given I didn't spot the clash. – Tom Wright Apr 11 '17 at 09:33

1 Answers1

5

The issue is that your method has a parameter enumVal (which has the same name as your model property). When the method executes, the DefaultModelBinder adds the value of the parameter to ModelState.

In the view the EnumDropDownListFor() method first checks for a match in ModelState (then ViewData and finally your model value), which it finds and attempts to set the selection option to the value in ModelState. For an explanation of why this is the default behavior, refer TextBoxFor displaying initial value, not the value updated from code.

In your case, the value of the parameter is either the value of the first option, or does not match any option value (and the first option is selected because something has to be).

Either change the name of the parameter so it is not the same as one of your model properties, or you can use ModelState.Clear(); to remove the value from ModelState.

Community
  • 1
  • 1