Asp Mvc使用默认参数

时间:2015-07-09 12:40:39

标签: asp.net asp.net-mvc asp.net-mvc-routing

我有这样的路线设置:

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会生成以下路径:

  • departmentType.Ops:<a href="/dashboard" class="active">OPS</a>
  • departmentType.Bps:<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"]
        };
    }

}

0 个答案:

没有答案