HTML.DropDownListFor 类分配

发布于 2024-12-13 06:58:06 字数 48 浏览 9 评论 0原文

是否可以将我自己的类分配给 HTML.DropDownListFor() 帮助器?

Is it possible to assign my own class to the HTML.DropDownListFor() helper?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霊感 2024-12-20 06:58:06

其中一个重载方法有一个 object htmlAttributes 作为最后一个参数,您可以在其中指定要添加到生成的 html 中的额外属性:

Html.DropDownListFor(..., new { "class" = "myclassname" };

http://msdn.microsoft.com/library/ee703670.aspx

One of the overloaded method has an object htmlAttributes as last parameter in which you can specify extra attributes to be added to the generated html:

Html.DropDownListFor(..., new { "class" = "myclassname" };

http://msdn.microsoft.com/library/ee703670.aspx

简单气质女生网名 2024-12-20 06:58:06

Didier的答案大部分是正确的,但是要在htmlAttributes对象中定义类值,您需要使用以下语法:

Html.DropDownListFor(..., new { @class = "myclassname" } );

“@”是让编译器知道这不是关键字“class”。使用 "class" 将导致编译错误。

Didier's answer is mostly correct, but to define a class value in htmlAttributes object, you need to use this syntax:

Html.DropDownListFor(..., new { @class = "myclassname" } );

The "@" is to let the compiler know that this is not the keyword "class". Using "class" will result in a compilation error.

回忆那么伤 2024-12-20 06:58:06

您可以使用以下任何逻辑。

@Html.DropDownList("CourseId", new List<SelectListItem>(), new {@class="myclassname"} )

@Html.DropDownList("CourseId", (IEnumerable<SelectListItem>)ViewBag.CourseId, new { @class="myclassname" })

@Html.DropDownListFor(x => x.CourseId, (IEnumerable<SelectListItem>)ViewBag.CourseId, new { @class = "myclassname" })

You can use any of the below logic.

@Html.DropDownList("CourseId", new List<SelectListItem>(), new {@class="myclassname"} )

OR

@Html.DropDownList("CourseId", (IEnumerable<SelectListItem>)ViewBag.CourseId, new { @class="myclassname" })

OR

@Html.DropDownListFor(x => x.CourseId, (IEnumerable<SelectListItem>)ViewBag.CourseId, new { @class = "myclassname" })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文