-1

I'm using @Html.EnumDropDownListFor() to display a dropdown list for my enums. I want to set the selected value to one of the enum values. The question asked here is very similar to what I'm asking here but the answer provided there, did not work in my case.

What I have tried so far :

Enum :

public enum Region
{
    Europe  = 0,
    NorthAmerica = 1,
    Korea = 2,
    China = 3,
    Taiwan = 4
}

Controller Action:

(where i set the value of my enum)

public ActionResult Index(Region region = Region.Europe)
{
    var model = new RealmViewModel();
    model.Realms = realmService.GetAllRealmsByRegion(region);
    model.Region = region;
    return View(model);
}

View Model:

public class RealmViewModel
{
    public IEnumerable<Realm> Realms { get; set; }
    public Region Region { get; set; }
}

View:

(where I'm showing the enum as dropdownlist and want to set the selected value)

@model RealmViewModel

<div class="form-group">
    <label for="region-input">Region</label>
    @Html.EnumDropDownListFor(model => model.Region, new { @class = "form-control", @id = "region-input" })
</div>

Inside the rendered view, the dropdown list is always set on the first enum value i.e Europe. Even when I'm calling Index() action with a realm other than Europe, and Debugger shows that Model.Region is indeed some other value like Korea, the drop down again shows Europe selected. Based on the other question, because I'm setting the model.Region value inside my controller index action, then the @Html.EnumDropDownListFor() should select that enum value as default, but for some reason it doesn't work in my case.

Why can't I set the EnumDropDownListFor selected value this way?

Update:

I'm calling the controller action like this : /Home/Index?region=Korea

@Ric's answer suggested to call the action like : /Home/Index?region=2

And then it worked as expected. So the real question should be: Why it doesn't work when I'm calling the action by the string value of the enum, but it only works when I call the action by the integer value of the enum?

I debugged both calling methods, and I can say that all the values in both methods are identical. So I don't understand why the first one doesn't work as expected.

Community
  • 1
  • 1
Sobhan
  • 1,051
  • 2
  • 13
  • 29

1 Answers1

1

By using the following as the url:

/Home/Index?region=1

The enum does indeed change value to NorthAmerica and is displayed correctly in the view.

If you added a new route:

routes.MapRoute(
                name: "DefaultRegion",
                url: "{controller}/{action}/{region}",
                defaults: new { controller = "Home", action = "Index", region = UrlParameter.Optional }
            );

And then using:

/Home/Index/3

The dropdownlist displays China.

View code:

@Html.EnumDropDownListFor(model => model.Region)

Ric
  • 12,855
  • 3
  • 30
  • 36
  • Hey @Ric, I'm calling the action via : `http://localhost:6115/Home/Index?region=Korea`, Inside `index()` I'm getting `Korea` as well. Does that mean the problem is where I'm using the string value rather than the number value of the enum? – Sobhan Jan 11 '16 at 15:14
  • I just tried it with enum number like you and it worked as expected. Do you know why it didn't work when I'm calling the action by the string value of the enum (like : `http://localhost:6115/Home/Index?region=Korea`)? I can't see the difference because the values shown in the debugger are identical. – Sobhan Jan 11 '16 at 15:17
  • Hi, I know what you mean, it does the same for me, maybe it's a bug? I'm trying to find out more information. – Ric Jan 11 '16 at 15:19
  • Here is some information: http://stackoverflow.com/questions/20043445/how-to-pass-enum-from-view-to-model-asp-net-mvc – Ric Jan 11 '16 at 15:22
  • I think the question and answers in that post are referring to "how to pass an enum from *view* to *controller*". Quite the opposite of my case where i'm passing an enum from *controller* to *view*. Still I don't understand why calling the action via `Home/Index?region=Korea' does not work where everything shown in debugger are identical to the way you call the action. – Sobhan Jan 11 '16 at 15:29
  • I think it still deals with the core issue of passing an enum and not binding properly. – Ric Jan 11 '16 at 15:56
  • I have just done a test and tried using `Html.DropdownlistFor()` and it works the way you want `region=china` is then correctly set. – Ric Jan 11 '16 at 16:03
  • Would you please explain how you changed the action parameters to make it work? Per the suggestion in the post you linked, my action is like : `public ActionResult Index(string region = "Europe")`, So i accept a string. Then i convert a string with `Region Region = (Region)Enum.Parse( typeof(Region), region, true);`. Then i called the action with `/Home/Index?region=Korea` but the result is still `Europe`. – Sobhan Jan 11 '16 at 16:58
  • Will add a sample when I get a chance – Ric Jan 11 '16 at 19:27