39

ビューに別の並べ替えとフィルタリングを適用したいのですが、クエリ文字列を介して並べ替えとフィルタリングのパラメーターを渡すことになると考えました。

@Html.ActionLink("Name", "Index", new { SortBy= "Name"})

この単純な構造により、並べ替えが可能になります。ビューはクエリ文字列でこれを返します:

?SortBy=Name

今、フィルタリングを追加したいのですが、クエリ文字列を最終的に

?SortBy=Name&Filter=Something

の既存のパラメータのリストに別のパラメータを追加するにはどうすればよいActionLinkですか? 例えば:

user requests /Index/

ビューには

 @Html.ActionLink("Name", "Index", new { SortBy= "Name"})

 @Html.ActionLink("Name", "Index", new { FilterBy= "Name"})

リンク:最初のものは次のように見え/Index/?SortBy=Name、2番目のものは /Index/?FilterBy=Name

ユーザーがフィルタリングを適用した後に並べ替えリンクを押したときに必要です-フィルタリングは失われないため、パラメーターを組み合わせる方法が必要です。私の推測では、クエリ文字列を解析するのではなく、MVC オブジェクトからパラメーターのコレクションを取得する方法があるはずです。

4

5 に答える 5

29

これまでのところ、私が見つけた最善の方法は、 のコピーを作成し、ViewContext.RouteData.Values それに QueryString 値を挿入することです。そして、すべての使用前にそれを変更しActionLinkます。.Union()常に辞書を変更する代わりに、使用方法を理解しようとしています。

<% RouteValueDictionary   tRVD = new RouteValueDictionary(ViewContext.RouteData.Values); %>

<% foreach (string key in Request.QueryString.Keys )
    {
         tRVD[key]=Request.QueryString[key].ToString();
    } %>

<%tRVD["SortBy"] = "Name"; %>
                <%= Html.ActionLink("Name", "Index", tRVD)%>
于 2009-02-27T12:11:13.097 に答える
12

私の解決策はqwerty1000のものに似ています。ActionQueryLink標準と同じ基本パラメーターを受け取る拡張メソッド を作成しましたActionLink。Request.QueryString をループし、RouteValuesまだ存在していないディクショナリに見つかったパラメーターを追加します (必要に応じて元のクエリ文字列を上書きできるようにします)。

既存の文字列を保持し、キーを追加しない場合の使用法は次のとおりです。

<%= Html.ActionQueryLink("Click Me!","SomeAction") %>

既存の文字列を保持し、新しいキーを追加するには、ユーザーは次のようになります。

<%= Html.ActionQueryLink("Click Me!","SomeAction", new{Param1="value1", Param2="value2"} %>

ActionLink以下のコードは 2 つの使用法のためのものですが、必要に応じて他の拡張機能と一致するように他のオーバーロードを追加するのは非常に簡単です。

    public static string ActionQueryLink(this HtmlHelper htmlHelper,
        string linkText, string action)
    {
        return ActionQueryLink(htmlHelper, linkText, action, null);
    }

    public static string ActionQueryLink(this HtmlHelper htmlHelper, 
        string linkText, string action, object routeValues)
    {
        var queryString =
            htmlHelper.ViewContext.HttpContext.Request.QueryString;

        var newRoute = routeValues == null 
            ? htmlHelper.ViewContext.RouteData.Values 
            : new RouteValueDictionary(routeValues);

        foreach (string key in queryString.Keys)
        {
            if (!newRoute.ContainsKey(key)) 
                newRoute.Add(key, queryString[key]);
        }
        return HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext,
            htmlHelper.RouteCollection, linkText, null /* routeName */, 
            action, null, newRoute, null);
    }
于 2010-01-22T06:06:55.143 に答える
8
<%= Html.ActionLink("Name", "Index", new { SortBy= "Name", Filter="Something"}) %>

クエリ文字列を保持するには、次のことができます。

<%= Html.ActionLink("Name", "Index", 
     String.IsNullOrEmpty(Request.QueryString["SortBy"]) ? 
        new { Filter = "Something" } : 
        new { SortBy=Request.QueryString["SortBy"], Filter="Something"}) %>

または、より多くのパラメーターがある場合は、Request.QueryString考慮して使用して手動でリンクを作成できます。

于 2009-02-27T08:58:09.567 に答える
4

ActionLinkCombinedの代わりに使用ActionLink

        public static string ActionLinkCombined(this HtmlHelper htmlHelper, string linkText, string actionName,
                                            object routeValues)
    {
        var dictionary = new RouteValueDictionary();
        foreach (var pair in htmlHelper.ViewContext.Controller.ValueProvider)
            dictionary[pair.Key] = pair.Value.AttemptedValue;
        if (routeValues != null)
        {
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(routeValues))
            {
                object o = descriptor.GetValue(routeValues);
                dictionary[descriptor.Name] = o;
            }
        }
        return htmlHelper.ActionLink(linkText, actionName, dictionary);
    }
于 2009-09-09T13:49:15.370 に答える
2

MVC4

@Html.ActionLink("link text","action",new { @id = 5, @name = "textName", @abc = "abc" })

また

 @Html.ActionLink("link text", "action", "controller", new { @id = 5, @name = "textName", @abc = "abc" }, new { @class = "cssClass" })

クエリ文字列は次のようになります。

yourDomainRout/action/5?name=textName&abc=abc

それは持っているだろうclass="cssClass"

于 2014-02-04T05:14:29.853 に答える