Adding CSS class to Html.BeginForm()
Asked Answered
M

3

90

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" })) 
{
}
Measurable answered 21/12, 2012 at 3:52 Comment(0)
H
171

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.
}
Highstepper answered 21/12, 2012 at 3:59 Comment(3)
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.Oldie
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" })Sphere
What does the 3rd parameter do? What's up with ReturnUrl?Juratory
M
1
Html.BeginForm("Index", "EIApplication", FormMethod.Post, new { enctype = "multipart/form-data", **@class = "nea-forms"** })
Manouch answered 2/7, 2019 at 9:8 Comment(1)
Please add a comment explaining how your code fixes the issue to improve this answer.Nimmons
C
-2

Or simply using html :

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

</form>
Countdown answered 4/5, 2017 at 13:49 Comment(2)
The question specifically states "Html.BeginForm()"Device
Also, I'm not sure if this will add the antiforgery token. Just something to keep in mind.Butz

© 2022 - 2024 — McMap. All rights reserved.