如何设置DropDownFor的ID和类-ASP.NET MVC [英] How to set id and class of DropDownFor - asp.net mvc

查看:89
本文介绍了如何设置DropDownFor的ID和类-ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小型mvc应用程序,并在其中使用DropDownListFor.我无法为该下拉列表设置ID和类.以下是我的DropdownListFor代码,我想在其中添加ID和类.感谢您的及时帮助,谢谢

I am creating a small mvc applicaiton and using DropDownListFor in it. Iam unable to set an id and class for that dropdownlist. Following is my DropdownListFor code in which i want to add Id and class. Timely help will be appreciated, thanks

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

推荐答案

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

@Html.DropDownListFor In html helpers If you want to add any html attribute you have to pass them as objects of htmlAttributes type , Like explained by MSDN

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

htmlAttributes在哪里

Where htmlAttributes are

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

因此,随着您的问题的进行

So as your question goes

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

其中new关键字为html属性创建对象

where new keyword creates an object for htmlattributes

这篇关于如何设置DropDownFor的ID和类-ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆