Asp.net mvc 4 如何为-asp.net mvc设置DropDownFor的id和类

Asp.net mvc 4 如何为-asp.net mvc设置DropDownFor的id和类,asp.net-mvc-4,Asp.net Mvc 4,我正在创建一个小型mvc应用程序,并在其中使用DropDownListFor。Iam无法为该dropdownlist设置id和类。下面是我的DropdownListFor代码,我想在其中添加Id和类。及时的帮助将不胜感激,谢谢 @Html.DropDownListFor(item => item.Student.StudentId, new SelectList(Model.StudentList, "StudentId", "FirstName"), "--Select--") 要设置

我正在创建一个小型mvc应用程序,并在其中使用DropDownListFor。Iam无法为该dropdownlist设置id和类。下面是我的DropdownListFor代码,我想在其中添加Id和类。及时的帮助将不胜感激,谢谢

@Html.DropDownListFor(item => item.Student.StudentId, new SelectList(Model.StudentList, "StudentId", "FirstName"), "--Select--")

要设置
@Html.DropDownListFor
id
class
,请使用以下代码:

@Html.DropDownListFor(item => item.Student.StudentId, new SelectList(Model.StudentList, "StudentId", "FirstName"), "--Select--",new { @id="myid", @class="myclass" })

为了在
@html.DropDownListFor
中设置html属性,您必须使用
new
关键字设置
@Html.DropDownListFor
的第四个参数,该关键字将为
htmlattributes

@Html.DropDownListFor
创建一个
对象,如果您要添加任何必须作为对象传递的Html属性,请在Html帮助程序中进行设置
htmlAttributes
类型,如

所以你的问题是

@Html.DropDownListFor(item => item.Student.StudentId, new SelectList(Model.StudentList, "StudentId", "FirstName"), "--Select--",new { @id="myid", @class="myclass" })
其中
new
关键字为htmlattributes创建一个对象

Type: System.Object
An object that contains the HTML attributes to set for the element
@Html.DropDownListFor(item => item.Student.StudentId, new SelectList(Model.StudentList, "StudentId", "FirstName"), "--Select--",new { @id="myid", @class="myclass" })