89

How to add class attribute for following situation (Using ReturnUrl only):

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
}

I want something like this:

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }, new { @class = "login-form" })) 
{
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Vishal
  • 1,262
  • 1
  • 12
  • 19

3 Answers3

170

There is an overload for that, if you supply the controller action, name, and form method.

@using (Html.BeginForm("ActionName", "ControllerName", 
            new { ReturnUrl = ViewBag.ReturnUrl }, 
            FormMethod.Post, new { @class="login-form" }))
{
  etc.
}
moribvndvs
  • 42,191
  • 11
  • 135
  • 149
  • 34
    The most common use of the method is `Html.BeginForm()`, therefore, I wish Microsoft would make it possible to call `Html.BeginForm( new{ @class="something"})` without the need to care about all other parameters. – Shadi Alnamrouti Apr 17 '17 at 13:53
  • 1
    Agree it would be nice to only specify htmlAttribute, however you don't have to specify **all** the params, just the method: `Html.BeginForm(FormMethod.Post, new { @class = "something" })` – Ben Robinson Jul 15 '20 at 20:45
  • 1
    What does the 3rd parameter do? What's up with ReturnUrl? – SKREFI Nov 15 '20 at 13:50
1
Html.BeginForm("Index", "EIApplication", FormMethod.Post, new { enctype = "multipart/form-data", **@class = "nea-forms"** })
mdexp
  • 3,492
  • 2
  • 9
  • 20
p4rds
  • 21
  • 6
-3

Or simply using html :

<form action="/ActionName/ControllerName" method="post" class="login-form">

</form>
Frédéric
  • 35
  • 1