189

ASP.NET MVC can generate HTML elements using HTML Helpers, for example @Html.ActionLink(), @Html.BeginForm() and so on.

I know I can specify form attributes by creating an anonymous object and pass that object for the (fourth in this case) htmlAttributes parameter where specifying an id for the element:

Html.BeginForm("Foo", "Bar", FormMethod.Post, new { id = "MyForm"})

But what about the class attribute? Obviously this does not work:

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.

I've also tried:

new { _class = "myclass"}

and

new { class_ = "myclass"}

But they also did not work, as the underscores get replaced by dashes.

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.

Community
  • 1
  • 1
Adrian Grigore
  • 33,034
  • 36
  • 130
  • 210

2 Answers2

351

In order to create an anonymous type (or any type) with a property that has a reserved keyword as its name in C#, you can prepend the property name with an at sign, @:

Html.BeginForm("Foo", "Bar", FormMethod.Post, new { @class = "myclass"})

For VB.NET this syntax would be accomplished using the dot, ., which in that language is default syntax for all anonymous types:

Html.BeginForm("Foo", "Bar", FormMethod.Post, new with { .class = "myclass" })
Community
  • 1
  • 1
  • The parser is not smart enough, IMO, if it needs that hint given the context it's in at that point in the code. – toddmo Jan 02 '17 at 18:09
2

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.

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.

Tim
  • 237
  • 1
  • 3
  • 4
    You are certainly right, however this case is different. I was looking for a way to specify metadata for a few jquery plugins. This is usually done by misusing the class attribute. – Adrian Grigore Jun 23 '09 at 15:54
  • 13
    You can suggest best practices, but if I have a template that uses css on a form, I'm not going to go butcher the template to make it "correct". Reality is, a customer isn't going to pay more because you did the right thing by css... – Christian Payne Aug 30 '12 at 04:58
  • 1
    @ChristianPayne They might be forced to pay more in the future because you didn't. – Ian Warburton Aug 19 '15 at 10:00
  • 2
    I down-voted this response because it does not answer the question. A more appropriate answer would answer the question first then give best practice guidance. – Ben Sep 25 '16 at 20:10