Skip to content Skip to sidebar Skip to footer

How Do I Get The Querystring Values Into A The Routevaluedictionary Using Html.beginform()?

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

Solution 1:

You could write a helper:

publicstatic MvcHtmlString QueryAsHiddenFields(this HtmlHelper htmlHelper)
{
    var result = new StringBuilder();
    var query = htmlHelper.ViewContext.HttpContext.Request.QueryString;
    foreach (string key in query.Keys)
    {
        result.Append(htmlHelper.Hidden(key, query[key]).ToHtmlString());
    }
    return MvcHtmlString.Create(result.ToString());
}

and then:

<% using (Html.BeginForm("Login", "Membership", null, FormMethod.Post, new { @class = "signin-form" })) { %>
    <%= Html.QueryAsHiddenFields() %>
<% } %>

Solution 2:

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 the formAction 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:

///<summary>/// Turn the current request's querystring into the appropriate param for <code>Html.BeginForm</code> or <code>Html.ActionLink</code>///</summary>///<param name="html"></param>///<returns></returns>///<remarks>/// See discussions:/// * http://stackoverflow.com/questions/4675616/how-do-i-get-the-querystring-values-into-a-the-routevaluedictionary-using-html-b/// * http://stackoverflow.com/questions/6165700/add-query-string-as-route-value-dictionary-to-actionlink///</remarks>publicstatic RouteValueDictionary QueryStringAsRouteValueDictionary(this HtmlHelper html)
{
    // shorthandvar qs = html.ViewContext.RequestContext.HttpContext.Request.QueryString;

    // because LINQ is the (old) new blackreturn qs.AllKeys.Aggregate(new RouteValueDictionary(html.ViewContext.RouteData.Values),
        (rvd, k) => {
            // can't separately add multiple values `?foo=1&foo=2` to dictionary, they'll be combined as `foo=1,2`//qs.GetValues(k).ForEach(v => rvd.Add(k, v));
            rvd.Add(k, qs[k]);
            return rvd;
        });
}

Post a Comment for "How Do I Get The Querystring Values Into A The Routevaluedictionary Using Html.beginform()?"