我有一条看起来像这样的路线:
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?
因此表单会发布一个如下所示的网址:
www.site.com/photo-gallery/1/selectedvalue
而不喜欢“
www.site.com/photo-gallery?filtertype=1&filtervalue=selectedvalue
谢谢!
答案 0 :(得分:1)
如果您只有一些参数存在,Mvc将创建一个查询字符串类型Url,除非它找到一个完全匹配的URL。
建议您需要以下内容:
routes.Add("testRoute",
new Route("/{action}/{controller}/{slug}/{filtertype}/{filtervalue}",
new { controller = "ControllerName", action = "PhotoGallery", slug = "photo-gallery", filtertype = UrlParameter.Optional, filtervalue = UrlParameter.Optional}
);
确保这在您现有路线之前