If my urls looks like: www.mysite.com/1/home www.mysite.com/1/other-action www.mysite.com/1/another-action/?value=a
如果我的网址如下:www.mysite.com/1/home www.mysite.com/1/other-action www.mysite.com/1/another-action/?value=a
how can I switch the 1 parameter while invoking the same action with the same parameters and querystring values?
如何在使用相同参数和查询字符串值调用相同的操作时切换1参数?
For example if I'm currently browsing www.mysite.com/1/another-action/?value=a I would like to obtain the necessary links to switch to www.mysite.com/2/another-action/?value=a . . www.mysite.com/x/another-action/?value=a
例如,如果我正在浏览www.mysite.com/1/another-action/?value=a,我想获得切换到www.mysite.com/2/another-action/?value=的必要链接。一个 。 。 www.mysite.com/x/another-action/?value=a
Url.Action seems not to help...
Url.Action似乎没有帮助......
2 个解决方案
#1
The general idea is clone the current RouteValueDictionary, change one value, and then build a new action link based on the new RouteValueDictionary. Here's an example. But you'd probably want to do this in a URL helper, rather than directly in the view:
一般的想法是克隆当前的RouteValueDictionary,更改一个值,然后基于新的RouteValueDictionary构建一个新的操作链接。这是一个例子。但您可能希望在URL帮助程序中执行此操作,而不是直接在视图中执行此操作:
<% var foo = new RouteValueDictionary();
foreach (var value in ViewContext.RouteData.Values)
{
foo.Add(value.Key, value.Value);
}
foo["id"] = 2;
var newUrl = Url.Action(foo["action"].ToString(), foo); %>
#2
I would propose small optimization to Craig's answer:
我建议对Craig的答案进行小优化:
<% var foo = new RouteValueDictionary(ViewContext.RouteData.Values); foo["id"] = 2; var newUrl = Url.Action(foo["action"].ToString(), foo); %>
<%var foo = new RouteValueDictionary(ViewContext.RouteData.Values); foo [“id”] = 2; var newUrl = Url.Action(foo [“action”]。ToString(),foo); %>
#1
The general idea is clone the current RouteValueDictionary, change one value, and then build a new action link based on the new RouteValueDictionary. Here's an example. But you'd probably want to do this in a URL helper, rather than directly in the view:
一般的想法是克隆当前的RouteValueDictionary,更改一个值,然后基于新的RouteValueDictionary构建一个新的操作链接。这是一个例子。但您可能希望在URL帮助程序中执行此操作,而不是直接在视图中执行此操作:
<% var foo = new RouteValueDictionary();
foreach (var value in ViewContext.RouteData.Values)
{
foo.Add(value.Key, value.Value);
}
foo["id"] = 2;
var newUrl = Url.Action(foo["action"].ToString(), foo); %>
#2
I would propose small optimization to Craig's answer:
我建议对Craig的答案进行小优化:
<% var foo = new RouteValueDictionary(ViewContext.RouteData.Values); foo["id"] = 2; var newUrl = Url.Action(foo["action"].ToString(), foo); %>
<%var foo = new RouteValueDictionary(ViewContext.RouteData.Values); foo [“id”] = 2; var newUrl = Url.Action(foo [“action”]。ToString(),foo); %>