开发者

asp.net mvc outbound routing from a GET form post

开发者 https://www.devze.com 2023-01-16 09:54 出处:网络
I have a route that looks like this: new { controller = \"ControllerName\", action = \"PhotoGallery\", slug = \"photo-gallery\", filtertype = UrlParameter.Optional, filtervalue = UrlParameter.Optiona

I have a route that looks like this:

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

and I have a form that looks like this:

  <%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)" })%>
  <%}%>

Right now when the form is submitted I get the form values appended to the url as query st开发者_开发问答rings (?filtertype=1 etc) Is there a way to get this form to use routing to render the URL?

So the form would post a URL that looked like this:

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

and not like"

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

Thanks!


If you only have some of the parameters present, Mvc will create a Query String type Url unless it finds an exact matching url.

Suggest you will need something like:

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

make sure this is before your existing route

0

精彩评论

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