2009-02-27 96 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 

它应该有​​

相关问题