在ASP.Net MVC 5應用程序中,我使用@ Hml.ActionLink助手在控制器上調用動作,在需要的地方傳遞兩個參數。但是,第二個參數總是以空值結束。使用@Hml.ActionLink將兩個參數傳遞給控制器,但第二個參數值始終爲空
這裏是視圖代碼,具有ActionLink的:
@Html.ActionLink(
linkText: "Remove",
actionName: "DeleteItemTest",
controllerName: "Scales",
routeValues: new
{
itemID = 1,
scaleID = 2
},
htmlAttributes: null
)
這裏是控制器的代碼:
public ActionResult DeleteItemTest(int? itemID, int? scaleID)
{
//...doing something here....
return View();
}
這是在頁面上結束了的HTML:
<a href="/scales/deleteitemtest/?itemID=1&scaleID=2">Remove</a>
在我的控制器中,我最後得到「itemID」爲1,而「scaleID」爲null。我究竟做錯了什麼?
更新 - 根據要求添加路由配置:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.AppendTrailingSlash = true;
routes.LowercaseUrls = true;
// Ignore .axd files.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Ignore everything in the Content folder.
routes.IgnoreRoute("Content/{*pathInfo}");
// Ignore everything in the Scripts folder.
routes.IgnoreRoute("Scripts/{*pathInfo}");
// Ignore the Forbidden.html file.
routes.IgnoreRoute("Error/Forbidden.html");
// Ignore the GatewayTimeout.html file.
routes.IgnoreRoute("Error/GatewayTimeout.html");
// Ignore the ServiceUnavailable.html file.
routes.IgnoreRoute("Error/ServiceUnavailable.html");
// Ignore the humans.txt file.
routes.IgnoreRoute("humans.txt");
// Enable attribute routing.
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
}
使用'@ Html.ActionLink'返回的值究竟是多少? – haim770
適合我的工作 - 生成'Remove' –
也適合我。他的問題可能是什麼? – Fabjan