Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass query parameter and class attribute to Html.BeginForm in MVC3?

I'm a little bit confused with Html helpers in MVC3.

I used this syntax when creating my forms before:

@using (Html.BeginForm("action", "controller", FormMethod.Post, new { @class = "auth-form" })) { ... }

this gives me

<form action="/controller/action" class="auth-form" method="post">...</form>

fine, that's what I needed then.

Now I need to pass ReturnUrl parameter to the form, so I can do it like this:

@using (Html.BeginForm("action", "controller", new { ReturnUrl="myurl" } )) { ... }

that would give me

<form action="/controller/action?ReturnUrl=myurl" method="post"></form>

but I still need to pass css class and id to this form and I can't find the way to do it simultaneously passing ReturnUrl parameter.

If I add FormMethod.Post it adds all my parameters as attributes to the form tag, without FormMethod.Post it adds them as query string parameters.

How do I do it?

Thanks.

like image 822
Burjua Avatar asked Apr 03 '12 11:04

Burjua


1 Answers

You can use:

@using (Html.BeginForm("action", "controller", new { ReturnUrl="myurl" }, FormMethod.Post, new { @class = "auth-form" })) { ... }

this will give:

<form action="/controller/action?ReturnUrl=myurl" class="auth-form" method="post">
   ...
</form>
like image 122
pjumble Avatar answered Oct 19 '22 23:10

pjumble