如何本地化枚举并使用类似于Html.SelectListFor <t> </t>的东西

时间:2010-12-15 14:36:20

标签: asp.net-mvc localization enums

让我说我得到了以下课程和枚举:

public class MyModel
{
    [DisplayName("Min egenskap")]
    public MyEnum TheProperty {get;set;}
}

public enum MyEnum
{
  [DisplayName("Inga från Sverige")]
  OneValue,

  [DisplayName("Ett annat värde")]
  AnotherValue
}

由于DisplayNameAttribute无法在枚举上使用,因此上述代码无效。是否有其他属性可以使用?

我想要做的是使用select之类的内容生成一个漂亮的html Html.SelectListFor(m => m.TheProperty)标记。该列表将在生成期间使用DisplayNameAttribute或类似属性。

通缉结果:

<select name="TheProperty">
<option value="OneValue">Inga från Sverige</option>
<option value="AnotherValue" selected="selected">Ett annat värde</option>
</select>

2 个答案:

答案 0 :(得分:4)

如何执行此操作的示例是使用枚举中的[Description]属性:

public enum DaysOfWeek
{
    [Description("Monday")]
    Monday = 1,

    [Description("Tuesday")]
    Tuesday = 2
}

然后创建这个EnumerationHelper类,它将允许您获取枚举的Description属性:

public static class EnumerationHelper
{
    //Transforms an enumeration description into a string 
    public static string Description<TEnum>(this TEnum enumObject)
    {
        Type type = enumObject.GetType();
        MemberInfo[] memInfo = type.GetMember(enumObject.ToString());

        if(memInfo != null && memInfo.Length > 0)
        {
            DescriptionAttribute[] attributes = (DescriptionAttribute[])memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

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

        return enumObject.ToString();

    }
}

然后,您可以查询您的枚举类以获取值和描述,然后构建一个SelectList。您必须在此类中引用EnumerationHelper:

var listOfDaysOfWeek = (from DaysOfWeek d in Enum.GetValues(typeof(DaysOfWeek))
                        select new { ID = d, Description = d.Description() });

viewModel.selectListDaysOfWeek = new SelectList(listOfDaysOfWeek, "ID", "Description");

最后在你看来:

<%: Html.DropDownListFor(m => m.DayOfWeek, Model.DaysOfWeek) %>

我希望这会有所帮助。

答案 1 :(得分:3)

我想在视图中显示Enum,所以我做了一个类似的Html帮助:

    /// <summary>
    /// Returns the [Description] value of a Enum member.
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="helper"></param>
    /// <param name="expression"></param>
    /// <returns></returns>
    public static MvcHtmlString DisplayEnumFor<TModel, TResult>(this HtmlHelper<TModel> helper, 
        Expression<Func<TModel, TResult>> expression) where TResult : struct {
        TResult value = expression.Compile().Invoke(helper.ViewData.Model);
        string propName = ExpressionHelper.GetExpressionText(expression);

        var description = typeof(TResult).GetMember(value.ToString())[0]
            .GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault();
        if (description != null) {
            return MvcHtmlString.Create((description as DescriptionAttribute).Description);
        }

        return MvcHtmlString.Create(value.ToString());
    }

用法:

@Html.DisplayEnumFor(m => m.SomeEnumProperty)