Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

c# - Add query string as route value dictionary to ActionLink

I'm trying to create an ActionLink to export data from a grid. The grid is filtered by values from the query string. Here's an example of the url:

http://www.mysite.com/GridPage?Column=Name&Direction=Ascending&searchName=text

Here's the code to add my ActionLink to the page:

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

When the link is displayed, the url is this:

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

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

I'm not sure this is the cleanest or most correct way but it does work

I didn't use your extension method. You'll have to reintegrate that:

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

then

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

Results in

Results

with class exportenter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...