C# 将查询字符串作为路由值字典添加到ActionLink
我正在尝试创建一个ActionLink来从网格导出数据。网格由查询字符串中的值过滤。以下是url的一个示例:C# 将查询字符串作为路由值字典添加到ActionLink,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在尝试创建一个ActionLink来从网格导出数据。网格由查询字符串中的值过滤。以下是url的一个示例: http://www.mysite.com/GridPage?Column=Name&Direction=Ascending&searchName=text 以下是将my ActionLink添加到页面的代码: @Html.ActionLink("Export to Excel", // link text "Export",
http://www.mysite.com/GridPage?Column=Name&Direction=Ascending&searchName=text
以下是将my ActionLink添加到页面的代码:
@Html.ActionLink("Export to Excel", // link text
"Export", // action name
"GridPage", // controller name
Request.QueryString.ToRouteDic(), // route values
new { @class = "export"}) // html attributes
显示链接时,url如下所示:
http://www.mysite.com/GridPage/Export?Count=3&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D
我做错了什么?试试这个:
我不确定这是最干净或最正确的方法,但它确实有效
我没有使用你的扩展方法。您必须重新整合:
@{
RouteValueDictionary tRVD = new RouteValueDictionary(ViewContext.RouteData.Values);
foreach (string key in Request.QueryString.Keys )
{
tRVD[key]=Request.QueryString[key].ToString();
}
}
然后
@Html.ActionLink(“导出到Excel”,//链接文本
“导出”,//操作名称
“GridPage”,//控制器名称
tRVD,
新字典{{“类”,“导出”}//html属性
导致
使用类导出如果您查看此处:
//您当前正在使用:
ActionLink(HtmlHelper、字符串、字符串、字符串、对象、对象)
//您要使用:
ActionLink(HtmlHelper、String、String、RouteValueDictionary、IDictionary)
从
这里有一个助手扩展,因此您可以在任何接受RouteValueDictionary
的方法中转储querystring
/// <summary>
/// Turn the current request's querystring into the appropriate param for <code>Html.BeginForm</code> or <code>Html.ActionLink</code>
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
/// <remarks>
/// See discussions:
/// * https://stackoverflow.com/questions/4675616/how-do-i-get-the-querystring-values-into-a-the-routevaluedictionary-using-html-b
/// * https://stackoverflow.com/questions/6165700/add-query-string-as-route-value-dictionary-to-actionlink
/// </remarks>
public static RouteValueDictionary QueryStringAsRouteValueDictionary(this HtmlHelper html)
{
// shorthand
var qs = html.ViewContext.RequestContext.HttpContext.Request.QueryString;
// because LINQ is the (old) new black
return qs.AllKeys.Aggregate(new RouteValueDictionary(html.ViewContext.RouteData.Values),
(rvd, k) => {
// can't separately add multiple values `?foo=1&foo=2` to dictionary, they'll be combined as `foo=1,2`
//qs.GetValues(k).ForEach(v => rvd.Add(k, v));
rvd.Add(k, qs[k]);
return rvd;
});
}
您也可以从
System.Collections.Specialized
中尝试CopyTo
,如下所示:
var queryParams = new Dictionary<string, object>();
Request.QueryString.CopyTo(queryParams);
var routeValueDictionary = new RouteValueDictionary(queryParams);
var queryParams=newdictionary();
Request.QueryString.CopyTo(queryParams);
var routeValueDictionary=新的routeValueDictionary(queryParams);
这里有一个固定版本,它还处理同一个键多次出现的情况:
public static RouteValueDictionary QueryStringToRouteValueDictionary(this HtmlHelper html)
{
var qs = html.ViewContext.RequestContext.HttpContext.Request.QueryString;
return qs.AllKeys.Aggregate(new RouteValueDictionary(html.ViewContext.RouteData.Values),
(rvd, k) =>
{
// get values
var values = qs.GetValues(k);
// if only one value simply add it
if (values.Length == 1)
{
rvd.Add(k, values[0]);
}
else
{
// an array has to be add using the syntax <k>[<i>] for the key values, where k is the key itself and i is the counter
for (var i = 0; i < values.Length; ++i)
{
rvd.Add($"{k}[{i}]", values[i]);
}
}
return rvd;
});
}
公共静态RouteValueDictionary QueryStringToRouteValueDictionary(此HtmlHelper html)
{
var qs=html.ViewContext.RequestContext.HttpContext.Request.QueryString;
返回qs.AllKeys.Aggregate(新的RouteValueDictionary(html.ViewContext.RouteData.Values),
(rvd,k)=>
{
//获取价值
var值=qs.GetValues(k);
//如果只有一个值,只需添加它即可
如果(values.Length==1)
{
rvd.加上(k,值[0]);
}
其他的
{
//必须使用键值的语法[]添加数组,其中k是键本身,i是计数器
对于(变量i=0;i
您的尝试不起作用的原因是.ToString()在Dictionary类上没有重载(也不应该重载,因为它用于存储的不仅仅是路由字典参数)。我认为这将把像a=1&a=2这样的查询字符串转换为a=1,2QueryString.GetValues解决了这个问题(它返回一个字符串数组),但我还不知道如何将其添加到RouteValueDictionary Hey@zod,你知道如何将它们添加到RouteValueDictionary吗?我找不到方法。@zod不幸的是,它是一个字典,所以它们必须是唯一的Entries不适用于我。尽管我在该参数中使用了RouteValueDictionary,但对象本身被序列化到Query字符串,不是我想要的键值对。
var queryParams = new Dictionary<string, object>();
Request.QueryString.CopyTo(queryParams);
var routeValueDictionary = new RouteValueDictionary(queryParams);
public static RouteValueDictionary QueryStringToRouteValueDictionary(this HtmlHelper html)
{
var qs = html.ViewContext.RequestContext.HttpContext.Request.QueryString;
return qs.AllKeys.Aggregate(new RouteValueDictionary(html.ViewContext.RouteData.Values),
(rvd, k) =>
{
// get values
var values = qs.GetValues(k);
// if only one value simply add it
if (values.Length == 1)
{
rvd.Add(k, values[0]);
}
else
{
// an array has to be add using the syntax <k>[<i>] for the key values, where k is the key itself and i is the counter
for (var i = 0; i < values.Length; ++i)
{
rvd.Add($"{k}[{i}]", values[i]);
}
}
return rvd;
});
}