views:

1343

answers:

2

Hi,

I know I can specify form attributes this way:

Html.BeginForm("DoSearch", "Search", FormMethod.Post, new { id = "MyForm"}))

But what about class attributes? Obviously this does not work:

Html.BeginForm("DoSearch", "Search", FormMethod.Post, new { class = "myclass"}))

I've also tried

new { _class = "myclass"}

and

new { class_ = "myclass"}

but it did not work.

Disclaimer: Yes, I know that I can just as well use a good old <form> element or wrap the form inside a <div class="...">, but I'd still be interested to know how it is supposed to be done.

+11  A: 
Html.BeginForm("DoSearch", "Search", FormMethod.Post, new { @class = "myclass"}))
Marwan Aouida
Perfect. Thanks! :-)
Adrian Grigore
A: 

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
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