Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
867 views
in Technique[技术] by (71.8m points)

asp.net mvc - Razor DropDownListFor: Adding Extra Attribute To SelectList Option Tag

I'm trying to create a select list. I've created it just fine using a collection from my viewmodel that allows me to set each option's value and text with the following code:

@Html.DropDownListFor(model => model.Networks, new SelectList(Model.Networks, "NetworkID", "Name"), new { @class="form-control" })

Model.Networks contains another property called CountryId. I'd like to add an attribute to each option tag so it looks like:

<option value="[NetworkId]" data-countryId="[CountryId]">Canada</option>

Which way should I go about doing this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can create a Form Helper class to create a custom drop down list, and create a custom 'selectListItem' class that has an extra property 'itemsHtmlAttributes' of type IDictionary - see below. You may need to play around with the 'id' or 'name' attributes to get the default model binding working. Below is a bit messy, I would suggest using TagBuilder to build the 'select' and 'option' tags:

public class SelectListItemCustom : SelectListItem
{
    public IDictionary<string, object> itemsHtmlAttributes { get; set; }
}

public static class FormHelper
{
    public static MvcHtmlString DropDownListForCustom(this HtmlHelper htmlHelper, string id, List<SelectListItemCustom> selectListItems)
    {
        var selectListHtml = "";

        foreach (var item in selectListItems)
        {
            var attributes = new List<string>();
            foreach (KeyValuePair<string, string> dictItem in item.itemsHtmlAttributes)
            {
                attributes.Add(string.Format("{0}='{1}'", dictItem.Key, dictItem.Value));
            }
            // do this or some better way of tag building
            selectListHtml += string.Format(
                "<option value='{0}' {1} {2}>{3}</option>", item.Value,item.Selected ? "selected" : string.Empty,string.Join(" ", attributes.ToArray()),item.Text);
        }
        // do this or some better way of tag building
        var html = string.Format("<select id='{0}' name='{0}'>{1}</select>", id, selectListHtml);

        return new MvcHtmlString(html);
    }
}

VIEW:

@{
    var item = new SelectListItemCustom { Selected = true, Value = "123", Text = "Australia", itemsHtmlAttributes = new Dictionary<string, object> { { "countrycode", "au" } } };
    var items = new List<SelectListItemCustom> { item };

    Html.Raw(Html.DropDownListForCustom("insertIdHere", items))
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...