开发者

ASP.NET MVC Custom dropdown from model with custom HtmlHelper

开发者 https://www.devze.com 2023-02-20 21:28 出处:网络
I am just having a play with HtmlHelpers in MVC, very useful stuff and now i am trying to create one for dropdowns

I am just having a play with HtmlHelpers in MVC, very useful stuff and now i am trying to create one for dropdowns based on passing in a model ( any ) what the property is for the value and same for text.

so i have something like this:

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 optio开发者_开发技巧n = new Tag("option")
                .With("value", (val).ToString())
                .AndIf(val.Equals(selectedValue), "selected", "selected")
                .WithText(itemText);
            dropDownBox.Append(option);
        }
        return MvcHtmlString.Create(dropDownBox.ToString());

    }

but this would only cover me for single fields but my model could be like this:

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
 }

so with above i want to then create something like this:

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

where the input is name of dropdown, name of value property, name of text property and default selected value

I have completed this with a little help from the answer below, this is the code for anyone interested:

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

and to run the code

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

thats it

many thanks


I know this doesn't answer your question directly, but I would suggest you move away from using strings for your name and ids. They are problematic and hard to maintain over time. Instead use Func.

Here's what I use for select lists:

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) + " ->");
    }

This is how it's used:

<%=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")%>
0

精彩评论

暂无评论...
验证码 换一张
取 消