开发者

Simple Html.Actionlink question (MVC3)

开发者 https://www.devze.com 2023-03-30 03:44 出处:网络
The Route: routes.MapRoute( \"Items\", // Route name \"{controller}/{action}/{id}\", // URL with开发者_如何学编程 parameters

The Route:

            routes.MapRoute(
            "Items", // Route name
            "{controller}/{action}/{id}", // URL with开发者_如何学编程 parameters
            new {controller = "Item", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );

The htmlhelper:

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

The link it generates:

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

What I want it to show:

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

How to do that?


Instead of using ActionLink what happens if you try the following?

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


You call Html.RouteLink (not Action Link) and map an additional route under your generic like this:

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

when you call RouteLink, you'll simply pass that "ChairsRoute" name

0

精彩评论

暂无评论...
验证码 换一张
取 消