将查询字符串作为路径值字典添加到ActionLink

Ste*_*ven 10 c# asp.net asp.net-mvc

我正在尝试创建一个ActionLink来从网格中导出数据.网格由查询字符串中的值过滤.这是url的一个例子:

http://www.mysite.com/GridPage?Column=Name&Direction=Ascending&searchName=text
Run Code Online (Sandbox Code Playgroud)

这是将ActionLink添加到页面的代码:

@Html.ActionLink("Export to Excel",    // link text
    "Export",                          // action name
    "GridPage",                        // controller name
    Request.QueryString.ToRouteDic(),  // route values
    new { @class = "export"})          // html attributes
Run Code Online (Sandbox Code Playgroud)

显示链接时,网址为:

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
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Khe*_*pri 24

试试这个:

我不确定这是最干净或最正确的方法,但确实有效

我没有使用你的扩展方法.你必须重新整合:

@{ 
    RouteValueDictionary tRVD = new RouteValueDictionary(ViewContext.RouteData.Values);
    foreach (string key in Request.QueryString.Keys ) 
    { 
        tRVD[key]=Request.QueryString[key].ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后

@Html.ActionLink("Export to Excel",    // link text
"Export",                          // action name
"GridPage",                      // controller name
tRVD, 
new Dictionary<string, object> { { "class", "export" } }) // html attributes
Run Code Online (Sandbox Code Playgroud)

结果是

结果

与类导出在此输入图像描述


Tre*_*Dev 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>)
Run Code Online (Sandbox Code Playgroud)