3

In my MVC3 project I have a controller "Test" with this method for Index action

Function Index(fields() As String) As ViewResult
...

Then on the View I have a a multi-select drop down list

@Html.ListBox("fields", New MultiSelectList(my_list, "Value", "Text")) 

and everything works fine, however when I try to create an Actionlink passing multiple values of parameter "fields" in the querystring like this

@Html.ActionLink("TestLink", "Index", New With {
         .fields = "value1&fields=value2"}) 

I get the following broken link in the HTML source with the querystring encoded

<a href="/Test/Index?fields=value1%26fields%3Dvalue2">TestLink</a> 

How can I pass multiple values to a querystring parameter by ActionLink?

Max
  • 4,965
  • 17
  • 49
  • 64
  • Out of curiosity, why do it as a single property instead of separating them out or making them part of a model? – krillgar Jan 21 '13 at 15:07
  • Ok, I can revert "fields" as a property of a model, then how can I make an ActionLink? – Max Jan 21 '13 at 15:13
  • It all depends on the model that you are currently using. You would just set them to hidden inputs or something similar. That might not be the best solution, especially if you are doing a dynamic list of fields where the number of fields you could be returning is not set. Either go with Darin's answer, or returning a JSON block may be the better bet. Let me know, and I could write up an answer with that for you. That would just give a string to pass as the parameter in a Javascript method call. – krillgar Jan 21 '13 at 15:27

1 Answers1

0

If you want to do this dynamically by fetching the values from the listbox simply put the listbox inside a form whose action is pointing the controller action you want to invoke and then use a submit button. HTML will then do the job for you.

If you want to generate an ActionLink you could use the following:

@Html.ActionLink(
    "TestLink", 
    "Index", 
    New RouteValueDictionary() From { 
        { "fields[0]", "value1" }, 
        { "fields[1]", "value2" } 
    }
)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928