【问题标题】:enum values in drop down list using ViewData and th viewcode to list it?使用 ViewData 和视图代码列出下拉列表中的枚举值?
【发布时间】:2013-06-18 09:06:33
【问题描述】:

如何在 ASP.NET MVC 4 中使用枚举值创建下拉列表?

我有一个Language 枚举:

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

而我的财产是:

public Language Language { get; set; }

我的控制器动作如下所示:

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

如何使用ViewData[] 通过下拉列表调用我的视图?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    这将返回

    Enum.GetNames(typeOf(Language ))
    
    
    English
    spanish
    Arabi
    

    还有这个

    Enum.GetValues(typeOf(Language ))
    
    
    1,2,3
    

    您可以查看语言列表:

    ViewBeg.Languages = Enum.GetNames(typeOf(Language)).ToList();
    

    【讨论】:

    • 您能否再详细说明一下。我的意思是如何使用 viewdata 传递它并在 html 中获取它?
    • 能否请您帮忙查看代码... 是这样的吗??
    • 您可以在模板中使用它:Html.DropDownListFor(o => o.EnumProperty, Enum.GetValues(typeof(enumtype)).Cast<enumtype>().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() }))
    • 最后一行ViewBeg而不是ViewBag有错字吗?
    【解决方案2】:

    我知道我迟到了,但是...查看我为此创建的帮助程序类...

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

    这个helpe可以如下使用:

    在控制器中:

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

    在视图中

    @Html.DropDownList("DropDownList")  
    

    助手:

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

    【讨论】:

    • 你给的链接我打不开。
    • 嗯?我只是自己尝试了一下,似乎工作正常......你得到了什么? 404?
    • 我收到“糟糕!Google Chrome 无法找到 www.ninjanye.co.uk”。我来自乌克兰,这是一个原因吗?
    • 奇怪...如果您手动输入www.ninjanye.co.uk...会发生什么?
    • 同样,也许 dns 解析器无法正常工作...你能给我你网站的 ip 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2011-04-11
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多