0

My html code is as follows

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

After this when i hover on the link rendered on browser it shows http://localhost/demo/department/index but when i change the index to Details in the actionLink parameter , then when i hover the link it shows http://localhost/demo/Employee/Details?id=2

Why in the first case instead of this http://localhost/demo/Employee/Index?id=2 , http://localhost/demo/department/index is coming.

I am very new to mvc. Please bear if this question is silly. Please help me.

UPDATE

My route file is

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{name}/{id}",
            defaults: new { controller = "Home", action = "Index", name = UrlParameter.Optional, id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "GetCountries",
            url: "{controller}/GetCountries",
            defaults: new { controller = "Home", action = "GetCountries" }
        );

        routes.MapRoute(
            name: "GetEmployeeDetailsOnId",
            url: "Employee/Details",
            defaults: new { controller = "Employee", action = "Details" }
        );


    }

Solution

routes.MapRoute(
               name: "GetEmployeeDetails",
               url: "Employee/Index/{deptId}",
               defaults: new { controller = "Employee", action = "Index", deptId = UrlParameter.Optional }
           );

Added this in route and its working.

tereško
  • 58,060
  • 25
  • 98
  • 150
shanmugharaj
  • 3,814
  • 7
  • 42
  • 70

1 Answers1

1

In the first case your parameters to ActionLink are

ActionName = "Details"
ControllerName = "Employee"

In the second case your parameters to ActionLink are

ActionName = "Index"
ControllerName = "Employee"

These parameters are then matched against your routes one by one.


In the first case there is a match against your third route (url: "Employee/Details")

In the second case there is a match against your first route (url: "{controller}/{action}/{name}/{id}")


For more information about how parameters are matched against routes, please see the link provided by @renjith in the comments: HTML.ActionLink method

Community
  • 1
  • 1
Svein Fidjestøl
  • 3,106
  • 2
  • 24
  • 40