2015-06-25 21 views
0

在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&amp;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 }); 
    } 
} 
+1

使用'@ Html.ActionLink'返回的值究竟是多少? – haim770

+3

適合我的工作 - 生成'Remove' –

+0

也適合我。他的問題可能是什麼? – Fabjan

回答

3

我看你使用屬性路由和MapMvcAttributeRoutes;你有這個路線映射?如果不是,則默認路由將優先,並且僅將第一個參數作爲ID。

您需要添加一個期望這兩個參數的路由。

像這樣的事情會被掌摑到控制器動作:

[Route("{itemID:int}/{scaleID:int}", Name = "DeleteItemTest")] 
public ActionResult DeleteItemTest(int? itemID, int? scaleID) 

請注意,這是不準確的代碼,只是一些從工作。

+1

太好了,非常感謝。這是我需要的。 –

相關問題