我想創建一個像/?name=Macbeth&year=2011
一個URL與我ActionLink
,我已經試過這樣做,像這樣:ActionLink的多參數
<%= Html.ActionLink("View Details", "Details", "Performances", new { name = item.show }, new { year = item.year })%>
,但它不工作。我該怎麼做呢?
我想創建一個像/?name=Macbeth&year=2011
一個URL與我ActionLink
,我已經試過這樣做,像這樣:ActionLink的多參數
<%= 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
)
除的MikaelÖstberg答案添加類似這在你的global.asax
routes.MapRoute(
"View Details",
"Performances/Details/{name}/{year}",
new {
controller ="Performances",
action="Details",
name=UrlParameter.Optional,
year=UrlParameter.Optional
});
然後在你的cont滾筒
// 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" } })
如何生成'/ Macbeth/2011'這樣的路徑? – bjan 2012-05-29 04:41:01