C# MVC ActionLink问题
我刚开始使用MVC,所以我想试试 我的ActionLink有问题:C# MVC ActionLink问题,c#,asp.net-mvc-4,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> } } 我的行动: public Acti
foreach (var item in areaList)
{
using (Html.BeginForm())
{
<p>
@Html.ActionLink(item.AreaName, "GetSoftware","Area", new { id = 0 },null);
</p>
}
}
我的行动:
public ActionResult GetSoftware(int AreaID)
{
return View();
}
我在这里查看了相同的问题,我在跟踪回答,但仍然是相同的错误。任何人都知道出了什么问题操作的参数名称不匹配。简单地说:
@Html.ActionLink(item.AreaName, "GetSoftware", "Area", new { AreaID = 0 }, null);
我认为这对您很有用。作为ActionLink helper的第四个参数发送的匿名类型必须具有与action方法参数同名的成员
@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>
}
}
foreach(区域列表中的变量项)
{
使用(Html.BeginForm())
{
@ActionLink(item.AreaName,//Title
“GetSoftware”,//ActionName
“区域”,//控制器名称
新的{AreaID=0},//路由参数
null//htmlArguments,它们是无的。您需要此值
//否则你调用了错误的方法。。。
);
}
}
您是否尝试过将新{id=0}
更改为新{AreaId=0}
这没有任何帮助,“id”参数仍然相同,仍然找不到。
public ActionResult GetSoftware(int id)
{
return View();
}
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>
}
}