I've found that Html.BeginForm()
automatically populates the routeValueDictionary with the RawUrl (ie. QueryStringParamters). However I need to specify an HtmlAttribute so I need to use the override...
public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes)
When I do the QueryString values are NOT automatically added to the RouteValueDictionary. How can I accomplish this?
Here is my best attempt but it doesn't seem to be working.
<% RouteValueDictionary routeValueDictionary = new RouteValueDictionary(ViewContext.RouteData.Values);
foreach (string key in Request.QueryString.Keys )
{
routeValueDictionary[key] = Request.QueryString[key].ToString();
}
using (Html.BeginForm("Login", "Membership", routeValueDictionary, FormMethod.Post, new { @class = "signin-form" }))
{%> ...
My Controller Action looks like this...
[HttpPost]
public ActionResult Login(Login member, string returnUrl)
{ ...
But the value of "returnUrl" which is part of the QueryString is always NULL unless I use the default parameterless Html.BeginForm() in my View.
Thanks, Justin
You could write a helper:
and then:
Inspecting the source code for
Html.BeginForm()
at http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/FormExtensions.cs doesn't help too much, but it shows the reason the parameterless method does what you want -- it's literally setting theformAction
from the request url.If you'd rather have the querystring remain as the querystring, rather than potentially be part of a POST, here's an alternative extension: