DropDownList引导样式

时间:2014-06-23 19:11:57

标签: c# css asp.net asp.net-mvc twitter-bootstrap

我是ASP.NET的新手,并且一直在asp.net网站上通过ASP.NET MVC 5入门教程。

我遇到了一个问题,我似乎无法使用bootstrap设置我的下拉框。我目前正在使用下面的代码,它可以在标准下拉列表中显示它,但我不确定如何使样式工作。

旧代码:

Genre: @Html.DropDownList("movieGenre", "All")

编辑:

@Paul找到解决方案:

@Html.DropDownList("movieGenre", (SelectList)ViewBag.movieGenre, "Select genre", new { @class = "form-control" })

感谢大家的帮助。

1 个答案:

答案 0 :(得分:17)

您需要应用CSS类表单控件。试试这个:

@Html.DropDownList("movieGenre", "All", new { @class = "form-control"})

同时尝试 DropDownListFor ,但必须明确设置模型属性:

@Html.DropDownListFor(model => model.MovieGenreModel, SelectList, new { @class = "form-control"})

感谢@Paul的纠正。


您可能需要 EXTEND 现有的HTML控件/更新重载/构造函数,如下所示:

public static MvcHtmlString DropDownList(
    this HtmlHelper htmlHelper,
    string name,
    IEnumerable<SelectListItem> selectList,
    string optionLabel,
    object htmlAttributes  //     <--------------- You need to add this here
)

有关MVC HTML Helpers的更多帮助,请查看:

  1. StackOverflow:Adding your own HtmlHelper in ASP.NET MVC
  2. Showing Enums in a dropdownlist on our ASP.NET MVC view
  3. Examining how ASP.NET MVC scaffolds the DropDownList Helper
  4. Drop Down Lists with Custom Data Elements
  5. StackOverflow:HtmlAttributes in Extension Method - Bootstrap & MVC 5

  6. 另一种选择是看看 TwitterBootstrapMVC

相关问题