1

I am fairly new to ASP.NET and I want to know if its possible to route a list element to an ACTIONLINK method?

In my View I am populating an unordered list and for each item in it has a link with some values I want to pass to the next view. I do this like so:

@model MyASPApplication.Models.FooViewModel

    <ul id="errors" class="collapse list-unstyled">
                    @for (int i = 0; i < Model.MyFooCollection.GroupBy(c => c.FooName).ToList().Count; i++)
                    {
                      <li>
                          @Html.ActionLink(Html.DisplayFor(model => model.MyFooCollection[i].LocationName).ToString(), "Index", "Foo", Model.MyFooCollection.ToList(), null)
                      </li>
                    }
                  </ul>

In my Foo Controller:

    public ActionResult Index(List<FooCollection> foo) //it has a value of 0
    {
      ...code logic here
      return View();
    }

When I run the application and place a breakpoint in my li element it doesn't fire when I click on its link. Am I not using the correct method? Many thanks in advance.

enter image description here

mmangual83
  • 151
  • 1
  • 2
  • 11
  • what do you mean by "route a list item"? Clicking on the li does nothing. Clicking on the hyperlink within it might. It may appear to be one and the same if your li has no padding or anything. You might think that's pedantic but it helps to be clear about what is happening. – ADyson Jan 29 '18 at 16:49
  • @ADyson I want to know how to pass a list (or collection) of FooCollection type to my next view – mmangual83 Jan 29 '18 at 16:53
  • Anyway it's hard to understand the logic of your code. You appear to be creating a whole list of hyperlinks which all point to the same thing? You also generally have problems if you transmit a whole list of items via GET request like this, the querystring might choke on it (size, or formatting). Check what your hyperlink actually looks like when rendered to the page. You'd probably be better POSTing a list of IDs...or maybe it needs a rethink. You seem to use the name from a single item of the collection as the description - are you trying to link to that single item? – ADyson Jan 29 '18 at 16:53
  • I guess here routing a list value to actionlink means ...you want to pass list to the action method .. right? If yes then should pass a new object model instead. – Planet-Zoom Jan 29 '18 at 16:54
  • @Ganesh yes, that is what I am trying to do. What would the syntax for that be? – mmangual83 Jan 29 '18 at 16:55
  • To elaborate, let's say your MyFooCollection contains 3 items, with the LocationName property set respectively "USA", "Germany", "Japan" in each. So you'll get 3 hyperlinks, each one displaying a different location name, right? So should the hyperlink take the user to a page which has some relation specifically to the individual item from MyFooCollection which relates to the location shown? That would seem to make more sense, unless I have not understood something? ... – ADyson Jan 29 '18 at 17:01
  • ... Don't you want to simply pass the ID of that item via the link, not the whole list? Why do you want to send all the data every time? If every link sends the whole list, what is the point of separate links? They will all link to the same page with the same data. – ADyson Jan 29 '18 at 17:02
  • @ADyson Added a visual flow – mmangual83 Jan 29 '18 at 17:21
  • you should think what @ADyson has asked.. by the way you can not pass list to action link.. as Action link is nothing but an anchor, it will just generate a url link to your controller action , and your parameters will be passed as query string. and I hope you dont want to pass whole list in your query string. – Planet-Zoom Jan 29 '18 at 17:21
  • Ok, from your flow...in View 1 construct the link so it will pass the ID of item 1 to your action method. Then use that ID to fetch the right values from the database relating to that item and display them within View 2. This kind of pattern is how web applications normally work, in general. – ADyson Jan 29 '18 at 17:26
  • @ADyson I'm new to ASP.NET how do you pass an Id in my actionlink? – mmangual83 Jan 29 '18 at 18:39
  • 1
    Well currently you're passing that list, so instead of that pass an ID. Replace `Model.MyFooCollection.ToList()` with `new { id = Model.MyFooCollection[i].ID }` (assuming that's the value containing the relevant ID, I don't know your object model, so you'll have to adjust as appropriate. Plenty of examples online already if you look. https://stackoverflow.com/questions/200476/html-actionlink-method is pretty comprehensive, for instance. Then the action method would be something like `public ActionResult Index(int id) {` – ADyson Jan 29 '18 at 22:12

0 Answers0