2009-02-27 149 views
39

我想有應用在我看來 不同的排序和篩選我想,我會通過查詢字符串路過排序和過濾PARAMS如何訪問asp.net mvc中的查詢字符串參數?

@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和第二個是/Index/?FilterBy=Name

我想當用戶按下排序鏈接a在他應用一些過濾 - 過濾不會丟失,所以我需要一種方法來結合我的參數。 我的猜測是應該有一種方法來解析查詢字符串,但從某些MVC對象獲取參數的集合。

+0

如何對該問題的剃刀語法更新? – MrBoJangles 2012-09-25 19:52:49

+1

你去..只花了我4年..( - : – 2016-12-09 20:29:57

回答

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)%> 
+0

另一個[問題](http://stackoverflow.com/questions/391023/make-namevaluecollection-accessible-to-linq-query/396504)表明,這是使用`@ Html.ActionLink`的最佳方式,我爲`Name`定下了 – Grastveit 2011-07-05 12:48:49

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考慮手動生成的鏈接。

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

我的解決方案類似於qwerty1000的。我創建了一個擴展方法ActionQueryLink,它採用與標準ActionLink相同的基本參數。它通過Request.QueryString循環,並將找到的所有參數添加到RouteValues字典中,但這些參數尚不存在(因此,如果需要,我們可以覆蓋原始查詢字符串)。

保留現有的字符串,但不添加任何鍵的用法是:

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

保留現有的字符串,並添加新鍵,用戶將是:

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

下面的代碼是兩種用法,但根據需要添加其他過載以匹配其他ActionLink擴展應該很容易。

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

MVC4

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

OR

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

查詢字符串會是這樣:

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

它應該有​​

相關問題