asp.net-mvc 如何将 class 属性添加到由 MVC 的 HTML Helpers 生成的 HTML 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/879266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add a class attribute to an HTML element generated by MVC's HTML Helpers?
提问by Adrian Grigore
ASP.NET MVC can generate HTML elements using HTML Helpers, for example @Html.ActionLink(), @Html.BeginForm()and so on.
ASP.NET MVC 可以使用 HTML Helpers 生成 HTML 元素,例如@Html.ActionLink(),@Html.BeginForm()等等。
I know I can specify form attributes by creating an anonymous objectand pass that object for the (fourth in this case) htmlAttributesparameter where specifying an idfor the element:
我知道我可以通过创建一个匿名对象来指定表单属性,并将该对象传递给(在这种情况下是第四个)htmlAttributes参数,其中id为元素指定一个:
Html.BeginForm("Foo", "Bar", FormMethod.Post, new { id = "MyForm"})
But what about the classattribute? Obviously this does not work:
但是class属性呢?显然这不起作用:
Html.BeginForm("Foo", "Bar", FormMethod.Post, new { class = "myclass"})
As that just throws random syntax errors when my view is requested, because it expects something else after encountering the C# keyword class.
因为这只是在请求我的视图时抛出随机语法错误,因为它在遇到 C# 关键字后期望其他内容class。
I've also tried:
我也试过:
new { _class = "myclass"}
and
和
new { class_ = "myclass"}
But they also did not work, as the underscores get replaced by dashes.
但它们也不起作用,因为下划线被 dash 代替。
I know that I can just as well write the HTML elements by hand or wrap the form inside a <div class="myClass">, but I'd still be interested to know how it is supposed to be done.
我知道我也可以手工编写 HTML 元素或将表单包装在 a 中<div class="myClass">,但我仍然有兴趣知道它应该如何完成。
回答by
In order to create an anonymous type (or any type) with a property that has a reserved keywordas its name in C#, you can prepend the property name with an at sign, @:
为了创建一个匿名类型(或任何类型),其属性在 C# 中以保留关键字作为其名称,您可以在属性名称前面加上一个 at 符号@:
Html.BeginForm("Foo", "Bar", FormMethod.Post, new { @class = "myclass"})
For VB.NET this syntax would beaccomplished using the dot, ., which in that language is default syntax for allanonymous types:
对于 VB.NET,此语法将使用点来完成.,在该语言中,它是所有匿名类型的默认语法:
Html.BeginForm("Foo", "Bar", FormMethod.Post, new with { .class = "myclass" })
回答by Tim
Current best practice in CSS development is to create more general selectors with modifiers that can be applied as widely as possible throughout the web site. I would try to avoid defining separate styles for individual page elements.
当前 CSS 开发中的最佳实践是创建更通用的带有修饰符的选择器,这些选择器可以在整个网站中尽可能广泛地应用。我会尽量避免为单个页面元素定义单独的样式。
If the purpose of the CSS class on the <form/>element is to control the style of elements within the form, you could add the class attribute the existing <fieldset/>element which encapsulates any form by default in web pages generated by ASP.NET MVC. A CSS class on the form is rarely necessary.
如果<form/>元素上的 CSS 类的目的是控制表单中元素的样式,则可以在现有<fieldset/>元素中添加 class 属性,该元素默认封装了 ASP.NET MVC 生成的网页中的任何表单。很少需要表单上的 CSS 类。

