【问题标题】:Simple Html.Actionlink question (MVC3)简单的 Html.Actionlink 问题 (MVC3)
【发布时间】:2011-08-24 13:54:06
【问题描述】:

路线:

            routes.MapRoute(
            "Items", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new {controller = "Item", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );

htmlhelper:

@Html.ActionLink("Chairs", "List", "Item", new {id="Chairs"}, null)

它生成的链接:

http://localhost:57899/Item/List?id=Chairs

我希望它显示的内容:

 http://localhost:57899/Item/List/Chairs

怎么做?

【问题讨论】:

  • ItemControllerList 操作上的签名是什么?
  • @David Signature?public ActionResult List(string id) { return View(_repository.GetItems(id)); }

标签: asp.net-mvc-3 asp.net-mvc-routing html.actionlink


【解决方案1】:

如果您尝试以下操作,而不是使用 ActionLink,会发生什么?

@Html.RouteLink("Items", new { id = "Chairs" })

【讨论】:

    【解决方案2】:

    您调用 Html.RouteLink(不是 Action Link)并在您的泛型下映射一条额外的路由,如下所示:

    routes.MapRoute(
            "ChairsRoute", // Route name
            "Item/List/{id}", // URL with parameters
            new {controller = "Item", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );
    

    当您调用 RouteLink 时,您只需传递“ChairsRoute”名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 2013-11-25
      • 2012-01-15
      • 1970-01-01
      相关资源
      最近更新 更多