空链路值在ActionLink中传递

Kha*_*han 1 asp.net-mvc razor asp.net-mvc-4

我是Asp.net MVC的新手.我想用这种方式创建Model的某些对象的超链接

  <ul>@foreach(Department department in @Model ) 
    {         
      <li>@Html.ActionLink(department.Name, "Index", "Employee", new {departmentid= department.Id },null)</li>   
    } </ul>
Run Code Online (Sandbox Code Playgroud)

现在如图所示,当我点击浏览器中的链接时,它应该移动到具有department.Id路由值的员工控制器的索引操作.但是当我单击链接时,它传递一个空路由值,但在URL中,它显示正确的价值.为什么会这样?有帮助吗?

这是Employee控制器中的Index Action

 public ActionResult Index(int id)
        {
            List<Employee> employees = new List<Employee>();

             employees.AddRange(db.Employees.ToList().Where(x => x.DepartmentId == id));

            return View(employees);
        }
Run Code Online (Sandbox Code Playgroud)

Lia*_*iam 6

你的行动呼吁错误.匿名对象(new {departmentid= department.Id })中的名称和参数名称必须匹配.更改departmentidid(因为您的操作需要一个名为id的参数Index(int id)):

@Html.ActionLink(department.Name, "Index", "Employee", new {id= department.Id },null)
Run Code Online (Sandbox Code Playgroud)