【问题标题】:Css class not working for Asp.Net MVC dropdownlistCss 类不适用于 Asp.Net MVC 下拉列表
【发布时间】:2018-03-20 02:42:39
【问题描述】:

我正在尝试为 asp.net mvc 下拉列表应用 css 类。它适用于案例编辑器,但不适用于案例下拉列表。我的代码中可能缺少哪些部分?

用于下拉菜单的工作 CSS 类

@Html.EditorFor(model => model.role, new { htmlAttributes = new { @class = "form-control" } })

下拉的 CSS 类失败

@Html.DropDownListFor(model => model.role, new SelectList((IEnumerable)ViewData["Roles"], "role1", "role1"), new { htmlAttributes = new { @class = "form-control" } })

【问题讨论】:

  • new { @class = "form-control" } 不是new { htmlAttributes = new { @class = "form-control" } }
  • DDLF CSS 属性只需使用new { @class = "form-control" },因为EditorFor 没有htmlAttributes 但DDLF 有。

标签: css asp.net asp.net-mvc


【解决方案1】:
//DropDownListFor: modelItem, selectList, optionLabel, htmlAttributes
//EditorFor: modelItem, additionalViewData

如果您“右键单击”代码并查看定义(在 Visual Studio 中),您将看到定义。因此,如果您使用它,它应该可以工作:

@Html.DropDownListFor(model => model.role, new SelectList((IEnumerable)ViewData["Roles"], "role1", "role1"), null, new { @class = "form-control" })

【讨论】:

  • ,null 和重载真的很有趣。谢谢。
  • 如果你输入“---请选择---”而不是null,你会在下拉列表中看到它的用途。
猜你喜欢
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 2014-04-05
  • 2019-03-20
  • 2012-05-27
相关资源
最近更新 更多