[英]MVC ActionLink issue
我是使用MVC的新手,所以我想尝试一下。
我的ActionLink有问题:
foreach (var item in areaList)
{
using (Html.BeginForm())
{
<p>
@Html.ActionLink(item.AreaName, "GetSoftware","Area", new { id = 0 },null);
</p>
}
}
GetSoftware是我的行动,Area是我的控制器。
我的错误:
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();
}
我在这里检查了同样的问题,我按照回答,但仍然是相同的错误。 任何人都知道怎么了
操作的参数名称不匹配。 只需使用:
@Html.ActionLink(item.AreaName, "GetSoftware", "Area", new { AreaID = 0 }, null);
@Html.ActionLink(item.AreaName, "GetSoftware","Area", new {AreaID = 0 },null);
@Html.ActionLink(item.AreaName, "GetSoftware","Area", new {AreaID = 0 },null);
我认为这对您有用。
作为ActionLink帮助器的第四个参数发送的匿名类型必须具有与操作方法参数同名的成员。
@Html.ActionLink("LinkText", "Action","Controller", routeValues: new { id = 0 }, htmlAttributes: null);
控制器类中的操作方法:
public ActionResult Action(int id)
{
// Do something. . .
return View();
}
您只需要更改操作方法的参数即可。 由于您的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 }
);
尝试这个
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>
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.