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.
Here's The way that worked for me
It was almost like there was a problem with overloading the method, but by specifying what things are, it seems to work fine...
I guess this doesn't directly answer the question, but why not just use a plain old form tag?
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"});
To create a RouteValueDictionary from the querystring:
Then you can use it with Html.BeginForm:
This works for me :
Explicit route values and method is what is required...
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.
Just incase you wanted to add other attributes as well. use below code