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

3 Answers 3

172

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.
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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" })
What does the 3rd parameter do? What's up with ReturnUrl?
1
Html.BeginForm("Index", "EIApplication", FormMethod.Post, new { enctype = "multipart/form-data", **@class = "nea-forms"** })

1 Comment

Please add a comment explaining how your code fixes the issue to improve this answer.
-2

Or simply using html :

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

</form>

2 Comments

The question specifically states "Html.BeginForm()"
Also, I'm not sure if this will add the antiforgery token. Just something to keep in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.