我有这样的路线设置:
context.MapRoute(
"Default",
"Dashboard/{controller}/{departmentType}/{action}/{year}/{month}",
new
{
controller = "Kpi",
departmentType = DepartmentType.Ops,
action = "Details",
year = DateTime.Today.Year,
month = DateTime.Today.Month
});
在视图中,当我使用Url.Action
时,Mvc会生成以下路径:
<a href="/dashboard" class="active">OPS</a>
<a href="/dashboard/kpi/bps" class="active">BPS</a>
Mvc使用我的OPS部门的默认路由,而不是填写我的路由中定义的{controller}
和{departmentType}
参数。这使得插件中的JavaScript无法使正确的a
- 标记处于活动状态。
有没有办法将Mvc配置为在生成路径时始终填写所有参数?
修改:按要求Url.Action
- 方法
<a href='@Url.Action("details", "kpi", RouteValues(DepartmentType.Ops))'>OPS</a>
<a href='@Url.Action("details", "kpi", RouteValues(DepartmentType.BPS))'>BPS</a>
@functions
{
object RouteValues(DepartmentType dt)
{
return new
{
DepartmentType = dt,
year = ViewContext.RouteData.Values["year"],
month = ViewContext.RouteData.Values["month"]
};
}
}