And*_*rew 5 asp.net-mvc url-routing redirecttoaction actionlink asp.net-mvc-routing
我有一个这样定义的动作:
public ActionResult Foo(int[] bar) { ... }
Run Code Online (Sandbox Code Playgroud)
像这样的网址将按预期工作:
.../Controller/Foo?bar=1&bar=3&bar=5
Run Code Online (Sandbox Code Playgroud)
我有另一个动作可以做一些工作,然后重定向到Foo上面的动作以获得bar.
是否有一种使用 RedirectToAction 或 ActionLink 指定路由值的简单方法,以便像上面的示例一样生成 url?
这些似乎不起作用:
return RedirectToAction("Foo", new { bar = new[] { 1, 3, 5 } });
return RedirectToAction("Foo", new[] { 1, 3, 5 });
<%= Html.ActionLink("Foo", "Foo", new { bar = new[] { 1, 3, 5 } }) %>
<%= Html.ActionLink("Foo", "Foo", new[] { 1, 3, 5 }) %>
Run Code Online (Sandbox Code Playgroud)
但是,对于数组中的单个项目,这些确实有效:
return RedirectToAction("Foo", new { bar = 1 });
<%= Html.ActionLink("Foo", "Foo", new { bar = 1 }) %>
Run Code Online (Sandbox Code Playgroud)
将 bar 设置为数组时,它会重定向到以下内容:
.../Controller/Foo?bar=System.Int32[]
Run Code Online (Sandbox Code Playgroud)
最后,这是使用 ASP.NET MVC 2 RC。
谢谢。
我不确定如何使用现有的助手来实现这一点。但您可以编写自己的方法来执行此操作。
这是我整理的一些东西:
public static string EnumerableActionLink(this HtmlHelper htmlHelper, string linkText, string controllerName, string actionName, IEnumerable enumerable, string variableName)
{
var builder = new StringBuilder(string.Format("/{0}/{1}?", controllerName, actionName));
foreach (var item in enumerable)
builder.Append(string.Format("{0}={1}&", variableName, item));
return string.Format("<a href=\"{0}\">{1}</a>", builder, linkText);
}
Run Code Online (Sandbox Code Playgroud)
使用示例:
<%= Html.EnumerableActionLink("Foo", "Foo", "Foo", new[] { 1, 3, 5 }, "bar")%>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2528 次 |
| 最近记录: |