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});
}