如何在ASP.NET MVC中访问查询字符串参数? [英] How do I access query string parameters in asp.net mvc?

查看:143
本文介绍了如何在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中已经存在的参数列表中添加另一个参数?例如:

How can I add another parameter to list of already existing ones in ActionLink? for Example:

user requests /Index/

视图具有

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

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

链接:第一个看起来像/Index/?SortBy=Name,第二个看起来是/Index/?FilterBy=Name

Links: The first one looks like /Index/?SortBy=Name and The second is /Index/?FilterBy=Name

我想要在用户应用某些过滤后按下排序链接时-过滤不会丢失,因此我需要一种组合参数的方法. 我的猜测是应该有一种不解析查询字符串,而是从某些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的副本 并将QueryString值注入其中. 然后在每次使用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)%>

这篇关于如何在ASP.NET MVC中访问查询字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆