asp.net-mvc - ActionLink 没有将参数传递给 Controller ​​——asp.Net MVC 4

标签 asp.net-mvc asp.net-mvc-4

拜托,我的一个 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/

相关文章:

c# - 如何在 ASP.net MVC4 的 webgird 中绑定(bind) JSON 返回结果值

asp.net-mvc - MVC actionlink posting 复杂类型列表

asp.net-mvc - 错误 System.Web.HttpCompileException (0x80004005)

asp.net-mvc - 在多个Web应用程序上使用ASP.Net Identity

c# - 指定布局时 MVC Controller 操作被调用多次

javascript - "The resource cannot be found"打印页面时

asp.net-mvc - 创建自定义 URL 路由,将管理部分与网站的其他部分分开

jquery - 如何在@Url.Action中传递动态值?

c# - MVC4 类数据注释 - 如何检查该字段不等于给定的字符串?

asp.net-mvc - 使用表单例份验证在ASP.NET MVC上的哪里存储已记录的用户信息?