【发布时间】:2011-08-01 09:56:19
【问题描述】:
我想用我的ActionLink 创建一个类似/?name=Macbeth&year=2011 的URL,我试过这样做:
<%= Html.ActionLink("View Details", "Details", "Performances", new { name = item.show }, new { year = item.year })%>
但它不起作用。我该怎么做呢?
【问题讨论】:
我想用我的ActionLink 创建一个类似/?name=Macbeth&year=2011 的URL,我试过这样做:
<%= Html.ActionLink("View Details", "Details", "Performances", new { name = item.show }, new { year = item.year })%>
但它不起作用。我该怎么做呢?
【问题讨论】:
您使用的重载使 year 值最终出现在链接的 html 属性中(检查您的渲染源)。
重载签名如下所示:
MvcHtmlString HtmlHelper.ActionLink(
string linkText,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes
)
您需要将两个路由值都放入 RouteValues 字典中,如下所示:
Html.ActionLink(
"View Details",
"Details",
"Performances",
new { name = item.show, year = item.year },
null
)
【讨论】:
/Macbeth/2011这样的路径?
除了 Mikael Östberg 的回答之外,在你的 global.asax 中添加类似这样的内容
routes.MapRoute(
"View Details",
"Performances/Details/{name}/{year}",
new {
controller ="Performances",
action="Details",
name=UrlParameter.Optional,
year=UrlParameter.Optional
});
然后在你的控制器中
// the name of the parameter must match the global.asax route
public action result Details(string name, int year)
{
return View();
}
【讨论】:
基于 Mikael Östberg 的回答,以防万一人们需要知道它如何处理 html attr。这里再举一个例子,参考自ActionLink
@Html.ActionLink("View Details",
"Details",
"Performances",
new { name = item.show, year = item.year },
new {@class="ui-btn-right", data_icon="gear"})
@Html.ActionLink("View Details",
"Details",
"Performances", new RouteValueDictionary(new {id = 1}),new Dictionary<string, object> { { "class", "ui-btn-test" }, { "data-icon", "gear" } })
【讨论】: