我是ASP.NET新手,并且一直在学习ASP.NET站点上的asp.net MVC5入门教程。
我遇到了一个问题,我似乎不能用bootstrap设置我的下拉框的样式。我目前正在使用下面的代码,它在一个标准的下拉列表中显示它,但我不确定如何让样式工作。
旧代码:
Genre: @Html.DropDownList("movieGenre", "All")
编辑:
@Paul找到了解决方案:
@Html.DropDownList("movieGenre", (SelectList)ViewBag.movieGenre, "Select genre", new { @class = "form-control" })
感谢大家的帮助。
发布于 2014-06-24 03:17:26
您需要应用CSS类表单控件。试试这个:
@Html.DropDownList("movieGenre", "All", new { @class = "form-control"})
也可以尝试DropDownListFor,,但必须显式设置模型属性:
@Html.DropDownListFor(model => model.MovieGenreModel, SelectList, new { @class = "form-control"})
感谢@Paul的更正。
您可能需要扩展现有的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并查看:
另一种选择是查看
https://stackoverflow.com/questions/24373558
复制相似问题