0

I have an enum like this:

public enum PaymentType
{
    Self=1,
    Insurer=2,
    PrivateCompany=3
}

And I am showing it as select box options like this inside Controller:

List<Patient.PaymentType> paymentTypeList =
    Enum.GetValues(typeof (Patient.PaymentType)).Cast<Patient.PaymentType>().ToList();
    ViewBag.PaymentType = new SelectList(paymentTypeList);

Here I can see that only the string part (example "Self") of the enum is going to the front end, so I won't get the value (example "1") of enum in my dropdown. How can I pass text as well as value of enum to select list?

3
  • You don't necessarily need to since the DefaultModelBinder will bind to the string value when you post back, but these answers show some methods to do it. Commented Mar 2, 2015 at 10:31
  • Just cast it to the int it will give you the int value. Commented Mar 2, 2015 at 10:31
  • Did you try searching? stackoverflow.com/questions/388483/… and so on. Commented Mar 2, 2015 at 10:32

3 Answers 3

5

You can write an extension method like this:

 public static System.Web.Mvc.SelectList ToSelectList<TEnum>(this TEnum obj)
            where TEnum : struct, IComparable, IFormattable, IConvertible // correct one
 {

   return new SelectList(Enum.GetValues(typeof(TEnum)).OfType<Enum>()
              .Select(x =>
                    new SelectListItem
                    {
                        Text = Enum.GetName(typeof(TEnum), x),
                        Value = (Convert.ToInt32(x)).ToString()
                    }), "Value", "Text");

}

and in action use it like this:

public ActionResult Test()
{
     ViewBag.EnumList = PaymentType.Self.ToSelectList();

     return View();
}

and in View :

@Html.DropDownListFor(m=>m.SomeProperty,ViewBag.EnumList as SelectList)

Rendered HTML:

<select id="EnumDropDown" name="EnumDropDown">
<option value="1">Self</option>
<option value="2">Insurer</option>
<option value="3">PrivateCompany</option>
</select>

Here is a working Demo Fiddle of Enum binding with DropDownListFor

Sign up to request clarification or add additional context in comments.

1 Comment

please clarify how to use this extension. so that it will be useful for others too
1
public enum PaymentType
{
        Self=1,
        Insurer=2,
        PrivateCompany=3
}

Get Self value:

int enumNumber = (int)PaymentType.Self; //enumNumber = 1

Exemple:

getEnum(PaymentType.Self);

private void getEnum(PaymentType t)
{
            string enumName = t.ToString();
            int enumNumber = (int)t;
            MessageBox.Show(enumName + ": " + enumNumber.ToString());
}

Comments

0

There is an extension method in MVC5 called SelectExtensions.EnumDropDownListFor which will generate the drop down list for you and bind the response back to an enum property in your model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.