2

I'm using MVC where I have a list of strings that I would like to point to a new page. I'm using Razor and am very new to MVC and cannot seem to find the answer to my question through google.

My list could contain of the following:

"hello"
"goodbye"
"seeya"

I know how to insert strings from the controller to the html page using ViewBag, and I would use the following actionlink if I had a fixed set of strings:

@Html.ActionLink("viewedName", "ChemicalClass", new { mystring = "hello" })
@Html.ActionLink("viewedName", "ChemicalClass", new { mystring = "goodbye" })
@Html.ActionLink("viewedName", "ChemicalClass", new { mystring = "seeya" })

As I understand it, this would generate 3 links that would redirect to the subpage "ChemicalClass", and it would contain the one of the 3 parameters, depending on the link that was clicked.

My quesiton is, how can I do the same, but have the ActionLinks created dynamically, since I won't know how many links are going to be created, nor the content of the strings. My goal is to show these links on the webpage in (preferabely) a list form, e.g.:

<ol>
    <li>
        hello
    </li>
    <li>
        goodbye
    </li>
    <li>
        seeya
    </li>
</ol>

Where each element in the list is a link and not just a string.

1
  • 2
    pass in a viewmodel with your links , then loop over them with razor Commented Jul 29, 2014 at 18:31

3 Answers 3

5

Create a view model that stores a collection of links

Model

public class ViewModel
{
    public IList<string> Links { get; set; }
}

populate that model in your controller

Controller

public ActionResult Index()
    {
        var model = new ViewModel
            {
                Links = new List<string>
                    {
                        "Hello",
                        "Goodbye",
                        "Seeya"
                    }
            };
        return View(model);
    }

and finally your view View

@model MvcApplication1.Models.ViewModel
        <ol>
        @foreach (var item in Model.Links)
        {
            <li>
            @Html.ActionLink("viewedName", "ChemicalClass", new { mystring = item })
            </li>
        }
        </ol>

Your class holds your collection of strings and razor loops over them to produce your links.

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

3 Comments

Thank you for this! I have one question though. If I am doing the same solution somehwere else in my project where I need the exact same model, can I reuse the code you have in the "model" section or should I create a new class for the new page?
If the view requires the same viewmodel then forsure you can reuse that view. You will need new controller methods however for the new view. But you can reuse the viewmodel
Okay thank you very much for this. It was extremely helpful. And I can say that I also made it work with several other pages!
0

you can use something like this.

                <ul>
                 @foreach (var x in Model)
                 {
                     <li><a href="#">@x.myString </a></li>
                  }
                </ul>

Comments

0

I have something like this in one of my Views:

@foreach (var item in Model.MyList)
    {
        <li>
            @Html.ActionLink("Select", "ActionName", "Chemical", new {id = item.AdmNum}, new { @class = "label label-info"})
        </li>
    }

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.