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.