加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 03:42:32 所属栏目:asp.Net 来源:网络整理
导读:我正在创建一个小的mvc应用程序并在其中使用DropDownListFor.我无法为该下拉列表设置id和类.以下是我想要添加Id和类的DropdownListFor代码.及时的帮助将不胜感激,谢谢 @Html.DropDownListFor(item = item.Student.StudentId,new SelectList(Model.StudentLis
我正在创建一个小的mvc应用程序并在其中使用DropDownListFor.我无法为该下拉列表设置id和类.以下是我想要添加Id和类的DropdownListFor代码.及时的帮助将不胜感激,谢谢

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

解决方法

@ Html.DropDownListFor在html帮助器中如果要添加任何html属性,则必须将它们作为htmlAttributes类型的对象传递,如 MSDN所解释

public static MvcHtmlString DropDownListFor<TModel,TProperty>(
    this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty>> expression,IEnumerable<SelectListItem> selectList,Object htmlAttributes
)

htmlAttributes的位置

Type: System.Object
An object that contains the HTML attributes to set for the element

所以你的问题就是这样

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

其中new关键字为htmlattributes创建一个对象

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读