我的HTML代码如下
<ul>
@foreach (Department department in @Model)
{
<li>
@Html.ActionLink(department.Name, "Index", "Employee", new { id = department.DeptId }, null)
</li>
}
</ul>
此后当我将鼠标悬停在浏览器上呈现的链接上时,显示http://localhost/demo/department/index
但是当我在actionLink参数中将索引更改为详细信息时,当我悬停链接时,它会显示http://localhost/demo/Employee/Details?id=2
为什么在第一种情况下而不是http://localhost/demo/Employee/Index?id=2
,http://localhost/demo/department/index
即将到来。
我对mvc很新。如果这个问题很愚蠢,请承担。 请帮帮我。
更新
我的路线文件是
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" }
);
}
解决方案
routes.MapRoute(
name: "GetEmployeeDetails",
url: "Employee/Index/{deptId}",
defaults: new { controller = "Employee", action = "Index", deptId = UrlParameter.Optional }
);
在路线及其工作中添加了这个。
答案 0 :(得分:1)
在第一种情况下,ActionLink
的参数为
ActionName = "Details"
ControllerName = "Employee"
在第二种情况下,ActionLink
的参数为
ActionName = "Index"
ControllerName = "Employee"
然后将这些参数逐一与您的路线匹配。
在第一种情况下,与第三条路线匹配(url: "Employee/Details"
)
在第二种情况下,您的第一条路线(url: "{controller}/{action}/{name}/{id}"
)
有关参数如何与路线匹配的更多信息,请参阅评论中@renjith提供的链接:HTML.ActionLink method