vote up 1 vote down
star

I have a method that returns an array (string[]) and I'm trying to pass this array of strings into an Action Link so that it will create a query string similar to:

/Controller/Action?str=val1&str=val2&str=val3...etc

But when I pass new { str = GetStringArray() } I get the following url:

/Controller/Action?str=System.String%5B%5D

So basically it's taking my string[] and running .ToString() on it to get the value.

Any ideas? Thanks!

flag
add comment

2 Answers

vote up -1 vote down

I'd use POST for an array.

link|flag
add comment
vote up 2 vote down

Try creating a RouteValueDictionary holding your values. You'll have to give each entry a different key.

<%  var rv = new RouteValueDictionary();
    var strings = GetStringArray();
    for (int i = 0; i < strings.Length; ++i)
    {
        rv["str" + i] = strings[i];
    }
 %>

<%= Html.ActionLink( "Link", "Action", "Controller", rv, null ) %>

will give you a link like

<a href='/Controller/Action?str0=val0&str1=val1&...'>Link</a>

You can then use the ValueProvider and select all of the keys that start with str.

public ActionResult Action()
{
    var strKeys = this.ValueProvider.Keys.Where( k => k.StartsWith("str") );

    foreach (string key in strKeys)
    {
        ...
    }
}
link|flag
add comment

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.