【问题标题】:adding a attributes to a custom html helper for drop down list将属性添加到下拉列表的自定义 html 助手
【发布时间】:2016-04-17 08:33:28
【问题描述】:

我在这里实现了解决方案: http://www.spicelogic.com/Journal/ASP-NET-MVC-DropDownListFor-Html-Helper-Enum-5

它只是从枚举成员中创建一个下拉列表。但是,我想将名称与下拉列表相关联,如下所示:

@Html.TextBox("username", Model.Username, new { id = "username" })

为了说明,我想以这样的方式使用它:

@Html.DropDownListFor(m => m.FavoriteColor, new { id = "username" } )

我该怎么做?如何扩展 EnumEditorHtmlHelper 类的 Extention 方法?

这里是EnumEditorHtmlHelper的定义:

namespace LojmanMVC.WebUI.Static
{
public static class EnumEditorHtmlHelper
{
    /// <summary>
    /// Creates the DropDown List (HTML Select Element) from LINQ 
    /// Expression where the expression returns an Enum type.
    /// </summary>
    /// <typeparam name="TModel">The type of the model.</typeparam>
    /// <typeparam name="TProperty">The type of the property.</typeparam>
    /// <param name="htmlHelper">The HTML helper.</param>
    /// <param name="expression">The expression.</param>
    /// <returns></returns>
    public static MvcHtmlString MyEnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        where TModel : class
    {
        TProperty value = htmlHelper.ViewData.Model == null 
            ? default(TProperty)
            : expression.Compile()(htmlHelper.ViewData.Model);
        string selected = value == null ? String.Empty : value.ToString();
        return htmlHelper.DropDownListFor(expression, createSelectList(expression.ReturnType, selected));
    }

    /// <summary>
    /// Creates the select list.
    /// </summary>
    /// <param name="enumType">Type of the enum.</param>
    /// <param name="selectedItem">The selected item.</param>
    /// <returns></returns>
    private static IEnumerable<SelectListItem> createSelectList(Type enumType, string selectedItem)
    {
        return (from object item in Enum.GetValues(enumType)
                let fi = enumType.GetField(item.ToString())
                let attribute = fi.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault()
                let title = attribute == null ? item.ToString() : ((DescriptionAttribute)attribute).Description
                select new SelectListItem
                {
                    Value = item.ToString(),
                    Text = title,
                    Selected = selectedItem == item.ToString()
                }).ToList();
    }
}

}

提前致谢。

【问题讨论】:

    标签: asp.net-mvc enums extension-methods


    【解决方案1】:

    您可以使用相同的方法执行此操作,但它需要 second parameterSelectListItemscollectionthird parameter 是您的 custom 属性。

    @Html.DropDownListFor(m => m.FavoriteColor, (IEnumerable<SelectListItem>)ViewBag.Colors, new {id="username" })
    

    【讨论】:

    • 很明显,我将在辅助方法中添加另一个参数,但是,我将在 EnumEditorHtmlHelper 类中进行必要的更改。
    • 这是您的自定义课程吗?
    猜你喜欢
    • 2015-07-07
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多