views:

248

answers:

1

I have a form with a collection of checkbox's for a refine search function on my website.

I am trying to pass an array in a form GET but the URL looks like:

/search?filter=foo&filter=bar&filter=green

Is there a better way to pass this in MVC? Possible like

/search?filter=foo,bar,green

Thanks in advance.

+1  A: 

There is now way you can change this URL. It is built by the browser.

You could change the URL by javascript before sending the request but the better way is to use the post redirect get pattern (PRG).

You first post to the controller and redirect to the url that you build in the controller. That way you have full controll over the URL.

EDIT

this is a sample

public ActionResult FilterResult()
{
RouteValueDictionary searchRoute = ControllerContext.RouteData.Values;
if (searchRoute["Filter"]==null)
{
    searchRoute.Add("Filter","");
}
searchRoute["Filter"] = "filter1,filter2,filter3";

return RedirectToAction("Search", searchRoute);
}
Malcolm Frexner
Hi Malcolm, I tried the PRG pattern but ran in to a problem. The array gets passed but I get .../search?filter=System.String[] when I do the redirect to 'Get' Action. Do you know why this happens? This is my post Action which is binded to a model and the model is then passed to the Get Action. public ActionResult Search(ResultsModel m) { ResultsModel model = new ResultsModel { Filters = m.Filters }; return RedirectToAction("Search", model); }
creativeincode
I updated the post
Malcolm Frexner