Khan
Khan

Reputation: 97

Null route value passes in ActionLink

I am new to Asp.net MVC. I want to create the hyper Links of some object of Model in this way

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

Now as it shows, when I click on Link in browser, it should move to Index action of employee controller with department.Id route value.But when I click the link, it passes a null route value, but in URL , it shows the correct value. Why is that like this? Any help?

this is the Index Action in Employee controller

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

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

            return View(employees);
        }

Upvotes: 1

Views: 1458

Answers (1)

Liam
Liam

Reputation: 29760

Your implmenting your action call wrong. the names in the anonymous object (new {departmentid= department.Id }) and the parameter names must match. Change departmentid to id (because your action expects a parameter called id Index(int id)):

@Html.ActionLink(department.Name, "Index", "Employee", new {id= department.Id },null)

Upvotes: 6

Related Questions