使用自定义HtmlHelper的模型的ASP.NET MVC Custom下拉列表

时间:2011-03-31 15:37:00

标签: asp.net asp.net-mvc asp.net-mvc-3 c#-4.0 html-helper

我只是在MVC中玩HtmlHelpers,非常有用的东西,现在我正在尝试创建一个用于下拉列表 基于传入模型(任何)的属性是什么属性值和文本相同。

所以我有这样的事情:

public static MvcHtmlString DataFilledDropDown<T>(this HtmlHelper h, string name, T selectedValue)
    {
        var myModel = typeof(T);
        var dropDownBox = new Tag("select").With("name", name).And("id", name);

        foreach (T val in Enum.GetValues(myModel))
        {
            var itemText = Resources.ResourceManager.GetString(val.ToString());
            if (String.IsNullOrEmpty(itemText)) itemText = val.ToString();

            var dft = new Tag("option")
                .With("value", "0")
                .AndIf(val.Equals(selectedValue), "selected", "selected")
                .WithText("-- CHOOSE --");
            dropDownBox.Append(dft);

            var option = new Tag("option")
                .With("value", (val).ToString())
                .AndIf(val.Equals(selectedValue), "selected", "selected")
                .WithText(itemText);
            dropDownBox.Append(option);
        }
        return MvcHtmlString.Create(dropDownBox.ToString());

    }

但是这只会覆盖我的单个字段,但我的模型可能是这样的:

public class TestModel{
     public int Id {get; set; } // this i want as a value in dropdown
     public string Name {get;set;} // this i want to be the text value in dropdown
     public bool isActive { get; set; } // this i dont need in dropdown but have data in model
 }

所以上面我想创建这样的东西:

Html.DataFilledDropDown<myModel>("TestDropdown","Id","Name","0")

其中输入是下拉列表名称,值属性名称,文本属性名称和默认选定值

我已经在下面的答案中提供了一些帮助,这是任何感兴趣的人的代码:

public static MvcHtmlString DataFilledDropDown<T>(
        this HtmlHelper h, string name, IEnumerable<T> items, 
        Func<T,string> valueField, Func<T,string> textField, string selectedValue)
    {
        var dropDownBox = new Tag("select").With("name", name).And("id", name);
        var defaultValue = "0";
        var dft = new Tag("option")
                .With("value", "0")
                .AndIf(defaultValue.Equals(selectedValue), "selected", "selected")
                .WithText("-- CHOOSE --");
        dropDownBox.Append(dft);

        foreach (var item in items)
        {
            var myValue = valueField(item);
            var myName = textField(item);

            var option = new Tag("option")
                .With("value", myValue)
                .AndIf(myValue.Equals(selectedValue), "selected", "selected")
                .WithText(myName);
            dropDownBox.Append(option);

        }

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

并运行代码

<%: Html.DataFilledDropDown("SpexOptionType", Model.Clubs, x => x.clubID.ToString(), x => x.clubName, "0")%>

多数民众赞成

非常感谢

1 个答案:

答案 0 :(得分:1)

我知道这不会直接回答你的问题,但我建议你不要使用字符串作为你的名字和ID。它们存在问题并且难以维持。而是使用Func。

这是我用于选择列表的内容:

public static string SelectList<T>(this HtmlHelper html, T root, IEnumerable<T> items, Func<T, string> itemValueContent,
                            Func<T, string> itemTextContent, Func<T, IEnumerable<T>> childrenProperty, Func<T, string> parentProperty, string selectSize)
    {
        StringBuilder parentSb = new StringBuilder();
        StringBuilder childSb = new StringBuilder();

        parentSb.AppendFormat("<select class='parent' name='parent' size='{0}'>\r\n", selectSize);
        childSb.AppendFormat("<select class='child' id='child' size='{0}'>\r\n", selectSize);

        foreach (T parentItem in items)
        {
            foreach (T item in childrenProperty(parentItem))
            {
                RenderParentOption(parentSb, item, itemValueContent, itemTextContent);

                foreach (T item1 in childrenProperty(item))
                {
                    RenderOptionWithClass(childSb, item1, itemValueContent, itemTextContent, parentProperty);
                }
            }                
        }



        parentSb.AppendLine("</select>");
        childSb.AppendLine("</select>");


        return parentSb.ToString() + childSb.ToString();
    }


    private static void RenderOptionWithClass<T>(StringBuilder sb, T item, Func<T, string> itemValueContent, Func<T, string> itemTextContent, Func<T, string> parentProperty)
    {
        sb.AppendFormat("<option class='sub_{2}' value='{0}'>{1}</option>\r\n", itemValueContent(item), itemTextContent(item), parentProperty(item));
    }

    private static void RenderParentOption<T>(StringBuilder sb, T item, Func<T, string> itemValueContent, Func<T, string> itemTextContent)
    {
        sb.AppendFormat("<option value='{0}'>{1}</option>\r\n", itemValueContent(item), itemTextContent(item) + " ->");
    }

这就是它的用法:

<%=Html.SelectList<HierarchyNode<CategoryModel>>(Model.CategoryList.First(), Model.CategoryList,
       x => x.Entity.RowId.ToString(),
       x => x.Entity.Name.ToString(),
       x => x.ChildNodes,
       x => x.Entity.ParentCategoryId.ToString(), "10")%>