2010-09-17 90 views
0

我有一個看起來像這樣的路線:asp.net的MVC出局路由

new { controller = "ControllerName", action = "PhotoGallery", slug = "photo-gallery", filtertype = UrlParameter.Optional, filtervalue = UrlParameter.Optional, sku = UrlParameter.Optional} 

,我有一個看起來像這樣的形式:

<%using(Html.BeginForm("PhotoGallery", "ControllerName", FormMethod.Get)) {%> 
    <%:Html.Hidden("filtertype", "1")%> 
    <%:Html.DropDownList("filtervalue", ViewData["Designers"] as SelectList, "Photos by designer", new { onchange = "selectJump(this)" })%> 
    <%}%> 

現在時表單提交我得到的表單值作爲查詢字符串附加到url(?filtertype = 1等)有沒有辦法讓這個表單使用路由來呈現URL?

所以形式會後,看起來像這樣的URL:

www.site.com/photo-gallery/1/selectedvalue 

,而不是像」

www.site.com/photo-gallery?filtertype=1&filtervalue=selectedvalue 

感謝

回答

1

如果你只有一些參數目前, Mvc將創建一個查詢字符串類型Url,除非它找到完全匹配的網址。

Sug武功你會需要這樣的東西:

routes.Add("testRoute", 
new Route("/{action}/{controller}/{slug}/{filtertype}/{filtervalue}", 
new { controller = "ControllerName", action = "PhotoGallery", slug = "photo-gallery", filtertype = UrlParameter.Optional, filtervalue = UrlParameter.Optional} 
); 

確保這是你現有的路線前

+0

感謝您的答覆!我最終使用PRG模式來獲得我需要的效果。我的路由很複雜,不允許有這樣的一般規則。 – Paul 2010-09-18 15:31:50