2

I have a search forms with pages that return a calendar. In top I have some search criteria and it all works fine if it's only one value but not if it's a list. When I want to build the url for the next month in my model I have something like this :

public ActionResult GetUrl()
    {
        var action = GetBaseAction();
        if (SelectedDivisions.Any()) action.AddRouteValue("SelectedDisions", SelectedDivisions.ToArray());
        if (RoomId.HasValue) action.AddRouteValue("RoomId", RoomId.Value);
        if (TeacherId.HasValue) action.AddRouteValue("TeacherId", TeacherId.Value);
        if (Month.HasValue) action.AddRouteValue("Month", Month.Value);
        if (Year.HasValue) action.AddRouteValue("Year", Year.Value);
        if (Day.HasValue) action.AddRouteValue("Day", Day.Value);

        return action;
    }

Wich add the parameters to the next month URL :

http://afi.local/coursesession/calendar?Month=9&Year=2012&Day=18&ViewType=weekly

but since you can select more than one division, it's a list of checkbox so when I post my form, I get this URL :

http://afi.local/coursesession/calendar?Month=9&Year=2012&Day=18&ViewType=weekly&SelectedDivisions=1&SelectedDivisions=2

The problem is that if I add 2 times the same keys it throw an exception and I don't know how to rebuild my URL with more than one division in the query string.

Thanks for the help!

VinnyG
  • 6,883
  • 7
  • 58
  • 76

3 Answers3

1

This is a possible duplicate:

how may i add integer list to route

The short answer is: you can't using the helper. You have to build it by hand.

Community
  • 1
  • 1
Antoine Leclair
  • 17,540
  • 3
  • 26
  • 18
  • Interesting but I'm working with an ActionResult so I can't do the Url.Action in the backend part... seems like I will have to change things around. – VinnyG Sep 11 '12 at 21:24
0

String.Join may be one option:

 String.Join(",", SelectedDivisions.ToArray())
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    It is `String.Join(",",SelectedDivisions.ToArray())` – L.B Sep 11 '12 at 21:16
  • your resulting URL would be http://afi.local/coursesession/calendar?SelectedDivisions=1,2&Year=2012&Month=9&Day=18&RoomId=&TeacherId=&Status=&ViewType=weekly this does not work – VinnyG Sep 11 '12 at 21:21
0

I found a solution here : https://stackoverflow.com/a/717732/245836

Basicly what I do is this :

var routeValues = new RouteValueDictionary();
            for (var i = 0; i < SelectedDivisions.Count; i++)
            {
                routeValues["SelectedDivisions[" + i + "]"] = SelectedDivisions[i];
            }
            action.AddRouteValues(routeValues);

And the resulting URL is not very clean but the bindings works fine :

http://afi.local/coursesession/calendar?SelectedDivisions%5B0%5D=1&SelectedDivisions%5B1%5D=2&SelectedDivisions%5B2%5D=3&Month=9&Year=2012&Day=11&ViewType=weekly

So now I can copy paste that URL and it will work for any search in my calendar.

Community
  • 1
  • 1
VinnyG
  • 6,883
  • 7
  • 58
  • 76