2013-07-20 53 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> 
    } 
}