2013-07-20 57 views
2

進出口新的使用MVC,所以我想我會試試看。MVC ActionLink的問題

我有一個問題,我的ActionLink:

foreach (var item in areaList) 
{ 
    using (Html.BeginForm()) 
    { 
     <p> 
     @Html.ActionLink(item.AreaName, "GetSoftware","Area", new { id = 0 },null); 
     </p> 
    } 
} 

GetSoftware是我的行動,面積是我的控制器。

我的錯誤:

The parameters dictionary contains a null entry for parameter 'AreaID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult GetSoftware(Int32) 

我的行動:

public ActionResult GetSoftware(int AreaID) 
{ 
    return View(); 
} 

我查了這裏同樣的問題,和IM之後的responces,但仍是同樣的錯誤。任何人有一個想法,什麼是錯

+1

您是否嘗試過改變'新{ID = 0}''到新的{areaID表示= 0}' –

回答

1

參數名稱的行爲不匹配。只需使用這樣的:

@Html.ActionLink(item.AreaName, "GetSoftware", "Area", new { AreaID = 0 }, null); 
0
@Html.ActionLink(item.AreaName, "GetSoftware","Area", new {AreaID = 0 },null); 
0
@Html.ActionLink(item.AreaName, "GetSoftware","Area", new {AreaID = 0 },null); 

我認爲這會爲你工作。

0

您要發送的ActionLink的幫手的第四個參數必須有成員相同的名稱爲您的操作方法參數的類型化名。在控制器類

@Html.ActionLink("LinkText", "Action","Controller", routeValues: new { id = 0 }, htmlAttributes: null); 

操作方法:

public ActionResult Action(int id) 
{ 
    // Do something. . . 

    return View(); 
} 
+0

這不會幫助可言,「ID」參數仍然是相同的,仍然不會被發現。 – Thousand

0

你只需要改變你的操作方法的參數。當你的ActionLink()就像follwoing:

@Html.ActionLink(item.AreaName, "GetSoftware", "Area", 
    routeValues: new { id = 0 }, htmlAttributes: null) 

,則應該更換控制器爲以下幾點:

public ActionResult GetSoftware(int id) 
{ 
    return View(); 
} 

這是默認的路由行爲。如果你堅持使用AreaID作爲參數,你應該聲明在RouteConfig.cs的路線,並把它之前的默認路由:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");    

     // some routes ... 

     routes.MapRoute(
      name: "GetSoftware", 
      url: "Area/GetSoftware/{AreaID}", 
      defaults: new { controller = "Area", action = "GetSoftware", AreaID = UrlParameter.Optional } 
     ); 

     // some other routes ... 

     // default route 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
0

試試這個

foreach (var item in areaList) 
{ 
    using (Html.BeginForm()) 
    { 
    <p> 
     @Html.ActionLink(item.AreaName, //Title 
        "GetSoftware",  //ActionName 
        "Area",    // Controller name 
        new { AreaID= 0 }, //Route arguments 
         null   //htmlArguments, which are none. You need this value 
             //  otherwise you call the WRONG method ... 
      ); 
    </p> 
    } 
}