How to set the selected value in EnumDropDownListFor?
Asked Answered
B

5

12

I'm using MVC 5.2.0 and I'm trying to use the new Html.EnumDropDownListFor. This is how I'm setting the values:

//Model
public class MyModel {
    public int SelectedEnumId { get; set; }
    public TestEnum MyEnum { get; set; }
}

//Enum
public enum TestEnum : int
{
    name1 = 1,
    name2 = 2
}

//View
@Html.EnumDropDownListFor(model => model.MyEnum,new { @class = "form-control" })

This is working and the values are being displayed. But how do I set the selected value (SelectedEnumId)?

Normally I would use

//Not enum
@Html.DropDownListFor(model => model.SelectedId, new SelectList(Model.myvalues, "Value", "Text"))

Is there a way to do this with the new Helper in MVC 5.1-5.2? Or I have to create a Extension method for this?

Birdman answered 16/7, 2014 at 1:24 Comment(0)
I
24

As far as I know just make sure the value you want to be selected is set in your Model before you call

//Controller:
...
myModel.TestEnum = TestEnum.name2;
...

//On your view
...
@Html.EnumDropDownListFor(model => model.TestEnum);
...
Isothermal answered 22/7, 2014 at 0:44 Comment(1)
This works, however it does not work when the enum field is nested, e.g. @Html.EnumDropDownListFor(model => model.Foo.TestEnum); will display the dropdown values, but nothing is ever selected.Abrade
S
4

Could NOT get the option selected in the controller to display on the front end either, so had to resort to setting a temporary hidden input and used jQuery to update on the client side:

<div class="form-group">
@Html.LabelFor(model => model.MyEnum, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
    @Html.EnumDropDownListFor(model => model.MyEnum, "Select name", new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.MyEnum, "", new { @class = "text-danger" })
</div>
@Html.Hidden("MyEnumTemp", (int)Model.MyEnum)

<script>
    $(function () {
        $("#MyEnum").val($("#MyEnumTemp").val());
    });
</script>
Shornick answered 4/5, 2016 at 13:27 Comment(2)
Just had the same issue in one of my views. Other views (using a different enum) work perfectly, but not this one. This hacky solution works, but I wish there was another solution and explanation.Verisimilitude
@Verisimilitude The problem is when the enum field is nested, e.g. @Html.EnumDropDownListFor(model => model.Foo.MyEnum);Abrade
C
0

Just Use This in Your Controller, It works like a charm

MyModel.TestEnum = (TestEnum)SelectedEnumId;

Not This will work fine assuming the case, that SelectedEnumId > 0.

if(SelectedEnumId > 0) {
   MyModel.TestEnum = (TestEnum)SelectedEnumId;
}
Catchment answered 15/12, 2018 at 18:28 Comment(0)
G
0
public class MyModel{
public int SelectedEnumId  { get { return Convert.ToInt32(this.MyEnum); } }
public TestEnum MyEnum { get; set; }
}
//add your model and after use DropDownList with EnumHelper, behaves asEnumDropDownListFor
@Html.DropDownListFor(model => model.SelectedEnumId, EnumHelper.GetSelectList(typeof(TestEnum)),new { @class="form-control" })
Goneness answered 4/9, 2021 at 10:5 Comment(2)
Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.Bumpkin
Please add further details to expand on your answer, such as working code or documentation citations.Organize
C
0

I know this thread is old, but I have same issue today and got it fixed; so just wanna share my codes.

you are so closed actually, all you need is to add EnumHelper.GetSelectList inside new SelectList.

//Model
public class MyModel {
    public int SelectedEnumId { get; set; }
}

//Enum
public enum TestEnum
{
    name1 = 1,
    name2 = 2
}

//View
// get default value
var selectedID = Model.MyModel.SelectedEnumId.ToString();

// dropdown list
@Html.DropDownListFor(model => model.MyModel.SelectedEnumId, new SelectList(EnumHelper.GetSelectList(typeof(TestEnum)), "Value", "Text", selectedID), new { @class = "form-control" })

EnumHelper.GetSelectList will assign Text and Value attributes to Enum class, and use SelectedValue property of SelectList for default selected option.

Hope this will help someone. Thanks

Carpi answered 25/1, 2023 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.