How to display a list of objects in an MVC View?

88,338

Solution 1

Your action method Service should return a View. After this change the return type of your Service() method from string to List<string>

public List<string> Service()
{
    //Some code..........
    List<string> Dates = new List<string>();
    foreach (var row in d.Rows)
    {
        Dates.Add(row[0]);
    }
    return Dates;
}

public ActionResult GAStatistics()
{
    return View(Service());
}

After this reference the model in your View:

@model List<string>
@foreach (var element in Model)
{
    <p>@Html.DisplayFor(m => element)</p>
}

In my example the ActionResult looks like this:

public ActionResult List()
{
    List<string> Dates = new List<string>();
    for (int i = 0; i < 20; i++)
    {
        Dates.Add(String.Format("String{0}", i));
    }
    return View(Dates);
}

Which resulted in the output:

enter image description here

Solution 2

You can do it as follows in the view,

@foreach (var item in @Model)      
{      
     <li>@item.PropertName</li>  
}   
Share:
88,338
WhoAmI
Author by

WhoAmI

me = "family" + "traveling" + "history" + "programming" + "coffee" + "guitar" + "gaming";

Updated on March 16, 2020

Comments

  • WhoAmI
    WhoAmI about 4 years

    I have a method that is returning a list of strings. I simply would like to display that list in a view as plain text.

    Here's the list from the controller:

    public class ServiceController : Controller
    {
    
        public string Service()
        {
            //Some code..........
            List<string> Dates = new List<string>();
            foreach (var row in d.Rows)
            {
                Dates.Add(row[0]);
            }
            return Dates.ToString();
        }
    
        public ActionResult Service()
        {
            Service();
       }
    }
    

    And the view:

    <table class="adminContent">
        <tr>
            <td>HEJ</td>
        </tr>
         <tr>
            <td>@Html.Action("Service", "Service")</td>
        </tr>
        </tr>
    </table>
    

    I figure I must do something in the view like a foreach loop and and reference the list using "@" but how?

  • WhoAmI
    WhoAmI about 10 years
    Actually this makes alot of sense. Thanks! Btw is the for-loop really nessesary? I mean, lets say we don't know how manny items are in the list can i do a foreach loop instead so that there arent a max result? Alternatively this: for (int i = 0; i != 0; i++) { Dates.Add(String.Format("String{0}", i)); } return View(Dates);
  • Marco
    Marco about 10 years
    The for loop in the controller is just for demo purposes to get some content in the List. Use whatever you like and whatever you need.
  • WhoAmI
    WhoAmI about 10 years
    Seems like i have a problem. I already have a model @model ServiceListModel Getting the error i can only have 1 model.
  • Marco
    Marco about 10 years
    Completly right. But there was no view in your question, so nobody knew about it. On the other hand: You have one controller action per view. One controller returns one model.