Using Html.BeginForm with querystring
Asked Answered
T

7

32

My url looks like this:

customer/login?ReturnUrl=home

In the login view, I have used this pattern of code which works fine.

@using(Html.BeginForm())
{
   ...
}

This magically generates following html

<form action="customer/login?ReturnUrl=home" method="post">

But now, I need to add an attribute (e.g., data-id="something") in the form. How can I do that? If I don't have any query string, I know I can do something like this:

@using(Html.BeginForm(action, controller, FormMethod.Post, 
                      new { data_id="something" }))

But don't know how to add querystring which should be in html:

<form action="customer/login?ReturnUrl=home" method="post" data-id="something">

I thought about using <form> directly but don't know how to specify querystring which is variable. And I have no idea how to achieve it with Html.BeginForm. Any tip would be appreciated.

RESOLUTION:

For now, I used <form> with following hint How to get current url value in View. The resulting view looks like

<form action="@Request.Url.PathAndQuery" data-id="something" method="POST">

But it would be nice to have an overloaded method of BeginForm for this.

Traditional answered 6/9, 2011 at 16:59 Comment(4)
where are you wanting to add the attribute, to the Form or something in the Form?Hooge
Are you looking to add another form value or to dynamically set the query string?Jennie
@Hooge and Tejs I want the attribute on form element.Traditional
[This][1] will surely help to solve with BeginForm [1]: #3077156Micrometry
F
17

I guess this doesn't directly answer the question, but why not just use a plain old form tag?

 <form action='customer/[email protected]["ReturnUrl"]' method="post" data-id="something">

Alternatively, you can create a custom HtmlHelperExtension that renders a form with path and querystring. In this HtmlHelperExtension you can iterate through your querystring values and populate the routeValueDictionary which you then pass to a Html.BeginForm constructor.

If you don't want something so extensible you can just use the overloaded constructor of Html.BeginForm using @Html.BeginForm("login", "customer", new {ReturnUrl = @Request.QueryString["ReturnUrl"]},FormMethod.Post, new {data-id="something"});

Fadiman answered 6/9, 2011 at 17:54 Comment(3)
i just noticed you edited your question with the first idea as well.Fadiman
But if I add another querystring, I have to change the code again. I got a better answer, please see the last part of my updated answer. Thanks for your answer.Traditional
You're welcome. I've updated my answer as well using a constructor of BeginForm itself.Fadiman
T
23

Here's The way that worked for me

Html.BeginForm("Profile", "Partner", routeValues: new {id=Partner.partner_id},method:FormMethod.Post)

It was almost like there was a problem with overloading the method, but by specifying what things are, it seems to work fine...

Trim answered 20/12, 2012 at 1:3 Comment(2)
Adding the query string parameters in the routeValues dictionary works for me.Roede
what if I want to add class to the form as well, @using (Html.BeginForm("actionName", "controllerName", routeValues: new { lang = "en" }, method:FormMethod.Post, htmlAttributes: new { @class= "my-form", enctype = "multipart/form-data" }))Ithnan
F
17

I guess this doesn't directly answer the question, but why not just use a plain old form tag?

 <form action='customer/[email protected]["ReturnUrl"]' method="post" data-id="something">

Alternatively, you can create a custom HtmlHelperExtension that renders a form with path and querystring. In this HtmlHelperExtension you can iterate through your querystring values and populate the routeValueDictionary which you then pass to a Html.BeginForm constructor.

If you don't want something so extensible you can just use the overloaded constructor of Html.BeginForm using @Html.BeginForm("login", "customer", new {ReturnUrl = @Request.QueryString["ReturnUrl"]},FormMethod.Post, new {data-id="something"});

Fadiman answered 6/9, 2011 at 17:54 Comment(3)
i just noticed you edited your question with the first idea as well.Fadiman
But if I add another querystring, I have to change the code again. I got a better answer, please see the last part of my updated answer. Thanks for your answer.Traditional
You're welcome. I've updated my answer as well using a constructor of BeginForm itself.Fadiman
D
17

To create a RouteValueDictionary from the querystring:

RouteValueDictionary queryStringDictionary = new RouteValueDictionary(Request.QueryString.AllKeys.ToDictionary(key => key, key => (object)Request.QueryString[key]));

Then you can use it with Html.BeginForm:

Html.BeginForm(null, null, queryStringDictionary, FormMethod.Post, new Dictionary<string, object> { { "autocomplete", "off" } })
Dibb answered 2/8, 2012 at 19:4 Comment(0)
B
3

using Reflector to look at the code,

BeginForm() will pass directly the rawUrl over to the final Form. Any other overloads on BeginForm will go through a helper utility which will strip the query string.

Bercy answered 2/4, 2015 at 17:37 Comment(0)
I
1

Just incase you wanted to add other attributes as well. use below code

@using (Html.BeginForm("actionName", "controllerName", routeValues: new { lang = "en" }, method:FormMethod.Post, htmlAttributes: new { @class= "my-form", enctype = "multipart/form-data" }))
Ithnan answered 20/10, 2017 at 19:51 Comment(0)
H
0

This works for me :

@using (Html.BeginForm("index", "Photos", routeValues: new { user = pUser, album = pAlbum, }, method: FormMethod.Get))

Explicit route values and method is what is required...

Harpsichord answered 23/8, 2017 at 3:42 Comment(0)
W
-1

Try @using(Html.BeginForm(null, null, FormMethod.Post, new { data_id="something" }))

It should use the default logic to construct the url, just as if you used BeginForm()

(never tried that though in such case, but I believe it should work)

Walton answered 6/9, 2011 at 17:23 Comment(2)
I tried that already but resulting form has action="/customer/login". It should be /customer/login?ReturnUrl=homeTraditional
Unfortunately, it does not preserve the query string when passing null to the action and controller parameters.Quintic

© 2022 - 2024 — McMap. All rights reserved.