1

Html.BeginForm()routeValueDictionaryにRawUrl(つまり、QueryStringParamters)が自動的に入力されることがわかりました。ただし、HtmlAttributeを指定する必要があるため、オーバーライドを使用する必要があります...

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

私が行うと、QueryString値はRouteValueDictionaryに自動的に追加されません。どうすればこれを達成できますか?

これが私の最善の試みですが、うまくいかないようです。

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

私のコントローラーアクションは次のようになります...

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

ただし、ビューでデフォルトのパラメータなしのHtml.BeginForm()を使用しない限り、QueryStringの一部である「returnUrl」の値は常にNULLです。

ありがとう、ジャスティン

4

2 に答える 2

5

ヘルパーを書くことができます:

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

その後:

<% using (Html.BeginForm("Login", "Membership", null, FormMethod.Post, new { @class = "signin-form" })) { %>
    <%= Html.QueryAsHiddenFields() %>
<% } %>
于 2011-01-13T07:54:36.300 に答える
3

http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/FormExtensions.csのソース コードを調べることHtml.BeginForm()はあまり役に立ちませんが、パラメーターなしのメソッドが何をするかを示しています。あなたが望む-それは文字通りリクエストURLから設定しています。formAction

潜在的に POST の一部になるのではなく、クエリ文字列をクエリ文字列のままにしたい場合は、別の拡張機能を次に示します。

/// <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;
        });
}
于 2014-10-23T16:42:03.707 に答える