Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
5  
Did you ever get an answer for this? – reach4thelasers Nov 29 '09 at 16:15

2 Answers

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?str=val0&str=val1&...'>Link</a>

EDIT: MVC2 changed the ValueProvider interface to make my original answer obsolete. You should use a model with an array of strings as a property.

public class Model
{
    public string Str[] { get; set; }
}

Then the model binder will populate your model with the values that you pass in the URL.

public ActionResult Action( Model model )
{
    var str0 = model.Str[0];
}
share|improve this answer
1  
Just thought I'd mention that it looks like you've given another alternative to a similar question over here at: [ASP.Net MVC RouteData and arrays] (stackoverflow.com/questions/1752721/…). Is there a way to link these two questions so that people can see both of your alternatives? – GuyIncognito Jan 20 '10 at 17:16
I think you just did. Actually this won't work any more. I'll update the action method to use a model. – tvanfosson May 30 '10 at 3:08
The model binding is not the issue. It seems MVC 2 still generates query strings like ?str=System.String%5B%5D when a RouteValueDictionary value contains an array/list/etc. Still no way around that? – Crescent Fresh May 31 '10 at 2:39
@Crescent - are you sure you're using the signature that has both route values and html attributes? – tvanfosson May 31 '10 at 3:45
3  
@tvanfosson: I believe so, yes. Passing route data like new { foo = new[] { "a", "b" } } to any of Html.ActionLink, Url.Action, HtmlHelper.GenerateLink, etc will generate a query string parameter like ?foo=System.String%5B%5D, rather than the expected ?foo=a&foo=b. Do you get different results if you specify html attributes? – Crescent Fresh May 31 '10 at 4:34
show 1 more comment

I'd use POST for an array. Aside from being ugly and an abuse of GET, you risk running out of URL space (believe it or not).

Assuming a 2000 byte limit. The query string overhead (&str=) reduces you to ~300 bytes of actual data (assuming the rest of the url is 0 bytes).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.