views:

46

answers:

2

I have a form that posts several like named elements to an action like so:

<%= Html.TextBox("foo") %>
<%= Html.TextBox("foo") %>
<%= Html.TextBox("foo") %>

posts to and returns:

public ActionResult GetValues(string[] foo)
{
    //code

    return RedirectToAction("Results", new { foo = foo })
}

the "Results" action then looks like this:

public ActionResult Results(string[] foo)
{
    //code

    return View()
}

The issue I'm having is that after the redirect my url looks like this:

/results?foo=System.String[]

instead of the intended:

/results?foo=value&foo=value&foo=value

Is there any way to get this to work with my current set-up?

A: 

Try changing the type of the parameter from string[] to IEnumerable<string>:

public ActionResult GetValues(IEnumerable<string> foo) { ... }

If you still have problems, on your form do something like:

<%= Html.TextBox("foo[0]") %>
<%= Html.TextBox("foo[1]") %>
<%= Html.TextBox("foo[2]") %>

That should do it.

Bruno Reis
Sorry for the delayed response... This didn't help. And just to clarify, I'm not having any issues getting the form values to bind to a string[] or IEnumerable... I'm having issues when I call RedirectToAction and pass it the string[] or IEnumerable.
DM
A: 

I didn't find a solution to work with the above code. What I ended up doing was taking the array/Enumerable, looping through it, and building a query string to pass with the redirect. Something along the lines of:

StringBuilder queryString = new StringBuilder();
for (int i = 1; i <= foo.Count(), i++)
{
    if (i == 1)
       queryString.Append("?foo=" + value); 
    else
        queryString.Append("&foo=" + value);
}

This is much more simplified than the code I ended up using, but it also allowed me to drop the null values that may be submitted with the form. Cleans things up a bit.

DM