Adding CSS class to Html.BeginForm()

Vishal picture Vishal · Dec 21, 2012 · Viewed 83.7k times · Source

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" })) 
{
}

Answer

moribvndvs picture moribvndvs · Dec 21, 2012

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