views:

24

answers:

1

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 strings (?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!

+1  A: 

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

Clicktricity
@Clicktricity- Thanks for the reply! I ended up using the PRG pattern to get the effect I needed. My routing is to complicated and would not allow for such a general rule.
Paul