How do I set a value for the default option with Html.DropDownList

后端 未结 4 1057
眼角桃花
眼角桃花 2020-12-20 20:20

I\'m using ASP MVC RC1.

A form I\'m using contains a dropdownlist which I have put in a view with this code.

<%= Html.DropDownList(\"areaid\", (Se         


        
4条回答
  •  温柔的废话
    2020-12-20 21:11

    I wanted to use the same SelectList for multiple drop downs and didn't want to duplicate the SelectList in the model so I just added a new Html Extension method that took in a value and set the selected item.

    public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, string value, IList selectList, object htmlAttributes)
    {
        IEnumerable items = selectList.Select(s => new SelectListItem {Text = s.Text, Value = s.Value, Selected = s.Value == value});
        return htmlHelper.DropDownList(name, items, null /* optionLabel */, htmlAttributes);
    }
    

提交回复
热议问题