Come faccio ad avere i valori QueryString in una della RouteValueDictionary utilizzando Html.BeginForm ()?

StackOverflow https://stackoverflow.com/questions/4675616

Domanda

Ho trovato che Html.BeginForm() popola automaticamente il RouteValueDictionary con l'RawUrl (es. QueryStringParamters). Tuttavia ho bisogno di specificare un HtmlAttribute quindi ho bisogno di usare l'override ...

public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes)

Quando faccio i valori QueryString NON sono automaticamente aggiunto al RouteValueDictionary. Come posso fare questo?

Qui è il mio miglior tentativo, ma non sembra funzionare.

    <% 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" }))
       {%> ...

Il mio Action Controller si presenta così ...

    [HttpPost]
    public ActionResult Login(Login member, string returnUrl)
    { ...

Ma il valore di "ReturnURL", che fa parte del QueryString è sempre NULL a meno che usi il default senza parametri Html.BeginForm (), a mio avviso.

Grazie, Justin

È stato utile?

Soluzione

Si potrebbe scrivere un aiutante:

public static 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());
}

e poi:

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

Altri suggerimenti

Controllo del codice sorgente per Html.BeginForm() a http : //aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/FormExtensions.cs non aiuta troppo, ma si vede il motivo per il metodo senza parametri fa ciò che si vuole - -. E 'letteralmente l'impostazione della formAction dalla richiesta URL

Se si preferisce avere la querystring rimanere come querystring, piuttosto che essere potenzialmente parte di un post, ecco un'estensione alternativa:

/// <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>
public static RouteValueDictionary QueryStringAsRouteValueDictionary(this HtmlHelper html)
{
    // shorthand
    var qs = html.ViewContext.RequestContext.HttpContext.Request.QueryString;

    // because LINQ is the (old) new black
    return 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;
        });
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top