gpt4 book ai didi

c# - DropDownList Bootstrap 样式

转载 作者:行者123 更新时间:2023-11-30 13:12:56 24 4
gpt4 key购买 nike

我是 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" })

感谢大家的帮助。

最佳答案

您需要应用 CSS 类 form-control。试试这个:

@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 并查看:

  1. 计算器溢出: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. 计算器溢出:HtmlAttributes in Extension Method - Bootstrap & MVC 5

另一种选择是查看 TwitterBootstrapMVC

关于c# - DropDownList Bootstrap 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24373558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com