2010-09-17 91 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