我正在尝试创建一个ActionLink来从网格中导出数据。网格由查询字符串中的值过滤。以下是网址的示例:
http://www.mysite.com/GridPage?Column=Name&Direction=Ascending&searchName=text
以下是将我的ActionLink添加到页面的代码:
@Html.ActionLink("Export to Excel", // link text
"Export", // action name
"GridPage", // controller name
Request.QueryString.ToRouteDic(), // route values
new { @class = "export"}) // html attributes
显示链接时,网址为:
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
我做错了什么?
答案 0 :(得分:24)
试试这个:
我不确定这是最干净或最正确的方法,但确实有效
我没有使用你的扩展方法。你必须重新整合:
@{
RouteValueDictionary tRVD = new RouteValueDictionary(ViewContext.RouteData.Values);
foreach (string key in Request.QueryString.Keys )
{
tRVD[key]=Request.QueryString[key].ToString();
}
}
然后
@Html.ActionLink("Export to Excel", // link text
"Export", // action name
"GridPage", // controller name
tRVD,
new Dictionary<string, object> { { "class", "export" } }) // html attributes
结果
使用类导出
答案 1 :(得分:7)
如果你看这里: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx
//You are currently using:
ActionLink(HtmlHelper, String, String, String, Object, Object)
//You want to be using:
ActionLink(HtmlHelper, String, String, String, RouteValueDictionary, IDictionary<String, Object>)
答案 2 :(得分:1)
How do I get the QueryString values into a the RouteValueDictionary using Html.BeginForm()?
的交叉发布这是一个帮助扩展,因此您可以在接受RouteValueDictionary
的任何方法中转储查询字符串。
/// <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;
});
}
答案 3 :(得分:0)
您也可以像这样从CopyTo
尝试System.Collections.Specialized
:
var queryParams = new Dictionary<string, object>();
Request.QueryString.CopyTo(queryParams);
var routeValueDictionary = new RouteValueDictionary(queryParams);
答案 4 :(得分:0)
这是一个固定版本,也可以处理相同密钥多次出现的情况:
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;
});
}