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!

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

String.Join may be one option:

 String.Join(",", SelectedDivisions.ToArray())

2 Comments

It is String.Join(",",SelectedDivisions.ToArray())
your resulting URL would be afi.local/coursesession/… this does not work
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.