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?

6
  • 1
    For starters, you are using the wrong overload .ActionLink(linkText, actionName, controllerName, htmlAttributes). Commented Feb 1, 2016 at 18:39
  • @Jasen Good catch. I've changed that. Commented Feb 1, 2016 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? Commented Feb 1, 2016 at 18:48
  • 2
    Try to see this stackoverflow.com/questions/1752721/… maybe can be useful to you. Commented Feb 1, 2016 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. Commented Feb 1, 2016 at 18:49

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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)
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.
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">
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.