0

How can I pass a list of parameters to a ActionLink all of which have the same name? I am using the default route.

I am looking for something like the following, but I know that anonymous objects can't have keys with the same name. Also, RoutingValueDictionary cannot have keys with the same name.

 @Html.ActionLink("Link Name", "Index", "Home", new {key="val1", key="val2", ... }, null)

Which would link to

localhost:8080/Home/Index?key=val1&key=val2& ...

How can I create this link?

tereško
  • 58,060
  • 25
  • 98
  • 150
mcnnowak
  • 265
  • 3
  • 17
  • 1
    For starters, you are using the wrong overload `.ActionLink(linkText, actionName, controllerName, htmlAttributes)`. – Jasen Feb 01 '16 at 18:39
  • @Jasen Good catch. I've changed that. – mcnnowak Feb 01 '16 at 18:41
  • Besides the compiler error here, using the same key name will cause other problems. What are you trying to solve with the identical key names? – Jasen Feb 01 '16 at 18:48
  • 2
    Try to see this http://stackoverflow.com/questions/1752721/asp-net-mvc-routedata-and-arrays maybe can be useful to you. – Joel R Michaliszen Feb 01 '16 at 18:48
  • Basically, I want to redirect to a page that will show data for multiple items, all of which have a key. Like selecting a subset of items from an index page. – mcnnowak Feb 01 '16 at 18:49
  • @JoelRamosMichaliszen so it's not built in anywhere then? I've got to use an extension method? Sounds a bit dodgy. How do sites like NewEgg etc. allow you to check boxes next to products, then compare them on another page? Is there some other method I could use to get this data to another page, without bothering with creating a URL containing all this data? – mcnnowak Feb 01 '16 at 18:53

2 Answers2

1

You will need to create the <a> tag manually

<a href="@Url.Action("Index", "Home")?key=val1&key=val2&key=val3">Link Name</a>

however from your comments your appear to be wanting to pass the values of checkboxes, in which case you can use a form with FormMethod.Get

@using (Html.BeginForm("Index", "Home", FormMethod.Get)
{
  <input type="checkbox" name="key" value="val1" />
  <input type="checkbox" name="key" value="val2" />
  <input type="checkbox" name="key" value="val3" />
  <input type="submit" value="Save" />
}

which will generate

.../Home/Index?key=val1&key=val3

assuming you check the 1st and 3rd checkboxes

  • Thanks! I'm going to use a modified version of your answer. Didn't know you could have different controls in a form with the same name. – mcnnowak Feb 01 '16 at 22:57
  • You can do that if you binding to a collection of simple value types (e.g. your method is `public ActionResult Index(string[] key)` but it would not work if your binding to a collection of complex objects (in which case you would need indexers) –  Feb 01 '16 at 23:00
  • Yes, I'm using `string[] key` in my controller, this should work just fine. I had everything working on my page with manually created URLs when I asked this question, just needed a way to produce the URL. – mcnnowak Feb 01 '16 at 23:01
0

You can't pass key, key, key, key and key. Because key = key! Instead pass a list.

Create a model

public class MyModel
{
    public List<string> Keys { get; set;}
}

Accept it in your controller

public ActionResult LinkName(MyModel model)
{
    // stuff
}

And pass from your view

@using (Html.Beginform("LinkName"))
{
    <input name="Keys[0]">
    <input name="Keys[1]">
    <input type="submit" value="submit">
}
Steve Harris
  • 5,014
  • 1
  • 10
  • 25