拜托,我的一个 Controller 现在有几个小时的问题了。我在整个项目中成功地使用了类似的方法。但是,由于某种原因,此 Controller 的参数始终为空。下面是我的 Controller 。
[Authorize(Roles = "Employer")]
public class EmployerController : Controller
{
[HttpGet]
public ActionResult Index(long? id)
{
}
我的操作链接在下面
<p>@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid})</p>
我使用了一个断点并确认我的 userid 不为空。
下面是我的路线。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: null,
url: "{controller}/Page{page}",
defaults: new { Controller = "Home", action = "Index" }
);
routes.MapRoute(
name: null,
url: "{Controller}",
defaults: new { Controller = "Home", action = "Index" }
);
routes.MapRoute(
name: null,
url: "{controller}/{action}/{id}/{title}",
defaults: new
{
controller = "Home",
action = "Index",
title=UrlParameter.Optional,
id = UrlParameter.Optional
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
routes.MapRoute(null, "{controller}/{action}");
}
}
我期望我的操作由路线四提供服务。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
以前我的 Controller 参数是 employerId,然后我虽然这可能是我错误的原因,因为我在路由中使用了 id。我更改为 id 但仍然是空的。我的网址返回类似内容。
Employer?Length=8
长度从何而来?请问我哪里出错了?任何帮助将不胜感激。
最佳答案
问题是您使用了错误的 @Html.ActionLink
重载。
代替
<p>@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid })</p>
尝试
<p>@Html.ActionLink("View jobs", "Index", "Employer",new { id = userid }, null)</p>
或
<p>@Html.ActionLink("View jobs", "Index", "Employer",new { id = userid }, new{})</p>
关于asp.net-mvc - ActionLink 没有将参数传递给 Controller ——asp.Net MVC 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28851084/