将 Bootstrap 类应用于 @Html.DropDownList()

本文关键字:DropDownList @Html 应用于 Bootstrap | 更新日期: 2023-09-27 17:56:29

我正在尝试将引导样式应用于以下下拉列表:

@Html.DropDownList("Scholarship_contactID", "-- Select Contact --", new { @class = "form-control"})

这将返回以下错误:

'System.Web.Mvc.HtmlHelper' 不包含 'DropDownList' 的定义和最佳扩展方法重载 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable, string)' 有一些无效的参数

将 Bootstrap 类应用于 @Html.DropDownList()

您尚未为下拉列表中的项目提供任何内容。

请注意错误的这一部分:

System.Collections.Generic.IEnumerable

..缺少可用于项的枚举项。将您的代码更改为以下内容:

@Html.DropDownList("Scholarship_contactID", 
                   new SelectList(new List<string>()), 
                   "-- Select Contact --", 
                   new { @class = "form-control"})

这将使用以下重载:

public static MvcHtmlString DropDownList(
    this HtmlHelper htmlHelper,
    string name,
    IEnumerable<SelectListItem> selectList,
    string optionLabel,
    object htmlAttributes
)