Search code examples
htmlasp.net-mvcasp.net-mvc-3razorhtml-helper

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.


Solution

  • 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>