4

How can I create a dropdown list using an enum value in ASP.NET MVC 4?

I have a Language enumeration:

public enum Language 
{
    English = 0,
    spanish = 2,
    Arabi = 3
}

And my property is:

public Language Language { get; set; }

And my Controller action looks like this:

[HttpPost]
public ActionResult Edit(tList tableSheet)
{         
    return RedirectToAction("Index");
}

How will I call in my view through a dropdown list using ViewData[]?

2 Answers 2

2

This will return

Enum.GetNames(typeOf(Language ))


English
spanish
Arabi

And this

Enum.GetValues(typeOf(Language ))


1,2,3

You can languages list to view:

ViewBeg.Languages = Enum.GetNames(typeOf(Language)).ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

COuld you elaborate it a little more. I mean how to pass it using viewdata and get it in html?
Could you please hlp m with the view code... Is it like??<@Html.DropDownList("Languages", (SelectList) ViewBag.Languages)
You could use this in your template: Html.DropDownListFor(o => o.EnumProperty, Enum.GetValues(typeof(enumtype)).Cast<enumtype>().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() }))
is there a typo on the last line ViewBeg instead of ViewBag?
2

I know i'm late to the party but... check out a helper class I created to do just this...

http://jnye.co/Posts/4/creating-a-dropdown-list-from-an-enum-in-mvc-and-c%23

This helpe can be used as follows:

In the controller:

//If you don't have an enum value use the type
ViewBag.DropDownList = EnumHelper.SelectListFor<Language>();

//If you do have an enum value use the value (the value will be marked as selected)
ViewBag.DropDownList = EnumHelper.SelectListFor(myEnumValue);

In the view

@Html.DropDownList("DropDownList")  

Helper:

public static class EnumHelper
{
    //Creates a SelectList for a nullable enum value
    public static SelectList SelectListFor<T>(T? selected)
        where T : struct
    {
        return selected == null ? SelectListFor<T>()
                                : SelectListFor(selected.Value);
    }

    //Creates a SelectList for an enum type
    public static SelectList SelectListFor<T>() where T : struct
    {
        Type t = typeof (T);
        if (t.IsEnum)
        {
            var values = Enum.GetValues(typeof(T)).Cast<enum>()
                             .Select(e => new { Id = Convert.ToInt32(e), Name = e.GetDescription() });

            return new SelectList(values, "Id", "Name");
        }
        return null;
    }

    //Creates a SelectList for an enum value
    public static SelectList SelectListFor<T>(T selected) where T : struct 
    {
        Type t = typeof(T);
        if (t.IsEnum)
        {
            var values = Enum.GetValues(t).Cast<Enum>()
                             .Select(e => new { Id = Convert.ToInt32(e), Name = e.GetDescription() });

            return new SelectList(values, "Id", "Name", Convert.ToInt32(selected));
        }
        return null;
    } 

    // Get the value of the description attribute if the 
    // enum has one, otherwise use the value.
    public static string GetDescription<TEnum>(this TEnum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        if (fi != null)
        {
            DescriptionAttribute[] attributes =
             (DescriptionAttribute[])fi.GetCustomAttributes(
    typeof(DescriptionAttribute),
    false);

            if (attributes.Length > 0)
            {
                 return attributes[0].Description;
            }
        }

        return value.ToString();
    }
}

10 Comments

The link given by you doesn't open for me.
Hmm? I just tried it myself and seemed to work fine... what do you get? a 404?
I got "Oops! Google Chrome could not find www.ninjanye.co.uk". I'm from Ukraine, could it be a reason?
Odd... what happens if you manually type in www.ninjanye.co.uk...?
The same, maybe dns resolver not working somehow... can you give me ip for your site?
|

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.