我想返回一个RedirectToRouteResult
将用户发送到如下所示的URL:
/MyController/MyAction/id?newid=3&newid=5&newid=7
newid
参数具有多个值。我的通话如下:
return RedirectToAction(string.Empty, routeValues);
这是到目前为止我尝试过的,但是不起作用:
// ...?newid%5B0%5D=3&newid%5B1%5D=5&newid%5B2%5D=7
var routeValues = new RouteValueDictionary {
{"id", myid},
{"newid[0]", 3},
{"newid[1]", 5},
{"newid[2]", 7},
};
// ...?newid=System.Int32%5B%5D
var routeValues = new { id = myid, newid = new int[] { 3, 5, 7 } };
// ...?newid=System.String%5B%5D
var routeValues = new { id = myid, newid = new string[] { "3", "5", "7" } };
// ...?newid=System.Int32%5B%5D
var routeValues = new RouteValueDictionary {
{"id", myid},
{"newid", new int[] { 3, 5, 7 } }
};
进行这项工作的秘诀是什么?
最佳答案
这是框架中真正缺少的一件事。最好的选择是手动滚动:
public ActionResult Foo()
{
var ids = new[] { 3, 5, 7 };
var url = new UriBuilder(Url.Action("MyAction", "MyController", new { id = "123" }, Request.Url.Scheme));
url.Query = string.Join("&", ids.Select(x => "newid=" + HttpUtility.UrlEncode(x.ToString())));
return Redirect(url.ToString());
}
当然,将其放入自定义扩展方法中可以提高可读性。
关于asp.net-mvc - 使用多值键创建RouteValueDictionary,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9295180/