简体   繁体   English

CSS类不适用于Asp.Net MVC下拉列表

[英]Css class not working for Asp.Net MVC dropdownlist

I am trying to apply css class for asp.net mvc dropdown list. 我试图将css类应用于asp.net mvc下拉列表。 It works for editorfor case but not for dropdownlistfor case. 它适用于case的editor,不适用于case的dropdownlist。 What may be the missing part from my code? 我的代码中可能缺少什么?

Working css class for dropdown 下拉菜单的工作CSS类

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

failing css class for drowdown 失败的CSS类失败

@Html.DropDownListFor(model => model.role, new SelectList((IEnumerable)ViewData["Roles"], "role1", "role1"), new { htmlAttributes = new { @class = "form-control" } })
//DropDownListFor: modelItem, selectList, optionLabel, htmlAttributes
//EditorFor: modelItem, additionalViewData

If you "right click" the code and peek definition (in Visual Studio) you'll see the definitions. 如果“右键单击”代码和查看定义(在Visual Studio中),您将看到这些定义。 So if you use this, it should work: 因此,如果您使用它,它应该可以工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM