Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Html.BeginForm with querystring

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.

like image 678
Tae-Sung Shin Avatar asked Sep 06 '11 16:09

Tae-Sung Shin


People also ask

How do you pass a route value in HTML BeginForm?

The value will only be added as query string values if its FormMethod. Get (and you can remove the route values from the BeginForm() method). But what is the point of this - they are hidden inputs, not editable values.

How does HTML BeginForm work?

BeginForm(HtmlHelper, String, String, FormMethod, Object)Writes an opening <form> tag to the response and sets the action tag to the specified controller and action. The form uses the specified HTTP method and includes the HTML attributes.

How do you pass a string in a QueryString?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.


1 Answers

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

like image 188
JustMaier Avatar answered Sep 19 '22 16:09

JustMaier