ActionLink 未将参数传递给控制器 -- asp.Net MVC 4
ActionLink is not passing parameter to the Controller -- asp.Net MVC 4
拜托,我的一个控制器几个小时以来一直有问题。我在整个项目中成功地使用了类似的方法。但是,由于某种原因,此控制器的参数始终为空。下面是我的控制器。
[Authorize(Roles = "Employer")]
public class EmployerController : Controller
{
[HttpGet]
public ActionResult Index(long? id)
{
}
}
我的操作Link在下方
<p>@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid})</p>
我使用了一个断点并确认我的userid不为null。
下面是我的路线。
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 但仍然是空的。我的 url 正在返回类似的内容。
Employer?Length=8
长度从何而来?请问我哪里出错了?任何帮助将不胜感激。
你必须使用这个 overload:
@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid}, null)
您目前正在使用 wrong overload,它假定控制器名称作为路由值。 (这是一个 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>
拜托,我的一个控制器几个小时以来一直有问题。我在整个项目中成功地使用了类似的方法。但是,由于某种原因,此控制器的参数始终为空。下面是我的控制器。
[Authorize(Roles = "Employer")]
public class EmployerController : Controller
{
[HttpGet]
public ActionResult Index(long? id)
{
}
}
我的操作Link在下方
<p>@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid})</p>
我使用了一个断点并确认我的userid不为null。
下面是我的路线。
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 但仍然是空的。我的 url 正在返回类似的内容。
Employer?Length=8
长度从何而来?请问我哪里出错了?任何帮助将不胜感激。
你必须使用这个 overload:
@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid}, null)
您目前正在使用 wrong overload,它假定控制器名称作为路由值。 (这是一个 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>