如何访问查询字符串PARAMS在asp.net mvc的? [英] how do i access query string params in asp.net mvc?
问题描述
我想有不同的排序和过滤应用在我的看法
我想,我会通过排序和过滤的 PARAMS 的通过查询字符串:
I want to have different sorting and filtering applied on my view I figured that I'll be passing sorting and filtering params through query string:
<%= Html.ActionLink("Name", "Index", new { SortBy= "Name"}) %>
这个简单的结构允许我进行排序。查看回来与此查询字符串中:
This simple construction allows me to sort. View comes back with this in query string:
?SortBy=Name
现在我想添加的过滤,我想我的查询字符串落得
Now I want to add filtering and i want my query string to end up with
?SortBy=Name&Filter=Something
我如何添加其他参数已经存在的列表中 ActionLink的
?例如:
user requests /Index/
查看有
<%= Html.ActionLink("Name", "Index", new { SortBy= "Name"}) %>
和
<%= Html.ActionLink("Name", "Index", new { FilterBy= "Name"}) %>
链接:第一个看起来像 /索引/ SortBy =名称
,第二个是 /首页/? ?FilterBy =名称
Links: The first one looks like /Index/?SortBy=Name
and The second is /Index/?FilterBy=Name
我想,当用户pressed分拣环节,他申请了一些筛选之后 - 过滤不会丢失,所以我需要一种方法来我的PARAMS结合起来。
我的猜测是应该有一种方法无法分析查询字符串,但记者从一些MVC的对象中获取的参数集合。
i want when user pressed sorting link after he applied some filtering - filtering is not lost, so i need a way to combine my params. My guess is there should be a way to not parse query string, but get collection of parameters from some MVC object.
推荐答案
到目前为止,我想出最好的办法是创建 ViewContext.RouteData.Values
的副本
并注入查询字符串值进去。
然后每 ActionLink的
使用前对其进行修改。
仍在试图弄清楚如何使用 .Union()
而不是修改字典所有的时间。
so far the best way I figured out is to create a copy of ViewContext.RouteData.Values
and inject QueryString values into it.
and then modify it before every ActionLink
usage.
still trying to figure out how to use .Union()
instead of modifying a dictionary all the time.
<% 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)%>
这篇关于如何访问查询字符串PARAMS在asp.net mvc的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!