MVC ActionLink问题

时间:2013-07-20 13:45:55

标签: c# asp.net-mvc-4

我是使用MVC的新手,所以我想我会尝试一下。

我的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();
}

我在这里检查了相同的问题,我跟着响应,但仍然是同样的错误。有人知道什么是错的

6 个答案:

答案 0 :(得分:1)

操作的参数名称不匹配。只需使用:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

我认为这对你有用。

答案 3 :(得分:0)

您作为ActionLink助手的第四个参数发送的匿名类型必须具有与您的操作方法参数同名的成员。

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

控制器类中的操作方法:

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

     return View();
}

答案 4 :(得分:0)

您只需要更改操作方法的参数即可。正如你的ActionLink()就像下面这样:

@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 }
        );  

答案 5 :(得分: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>
  }
}
相关问题