Nur*_*ihu 3 asp.net-mvc asp.net-mvc-4
拜托,我的一个控制器出现了几个小时的问题。我在整个项目中成功地使用了类似的方法。但是,出于某种原因,该控制器的参数始终为空。下面是我的控制器。
[Authorize(Roles = "Employer")]
public class EmployerController : Controller
{
[HttpGet]
public ActionResult Index(long? id)
{
}
Run Code Online (Sandbox Code Playgroud)
}
我的操作链接在下面
<p>@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid})</p>
Run Code Online (Sandbox Code Playgroud)
我使用了一个断点并确认我的用户 ID 不为空。
下面是我的路由。
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}");
}
}
Run Code Online (Sandbox Code Playgroud)
我期待我的行动能被路线四服务。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
Run Code Online (Sandbox Code Playgroud)
以前我的控制器参数是雇主 ID,然后我认为这可能是我错误的原因,因为我在路由中使用了 id。我更改为 id ,但仍然是空的。我的网址正在返回类似的东西。
Employer?Length=8
Run Code Online (Sandbox Code Playgroud)
长度从哪里来?请问我哪里出错了?任何帮助,将不胜感激。
问题是您使用了错误的@Html.ActionLink.
代替
<p>@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid })</p>
Run Code Online (Sandbox Code Playgroud)
尝试
<p>@Html.ActionLink("View jobs", "Index", "Employer",new { id = userid }, null)</p>
Run Code Online (Sandbox Code Playgroud)
或者
<p>@Html.ActionLink("View jobs", "Index", "Employer",new { id = userid }, new{})</p>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3582 次 |
| 最近记录: |