Devin Andres Salemi
Devin Andres Salemi

Reputation: 2206

.NET MVC razor -- iteration, and then iteration again?

I'm in C# .NET MVC. I am able to use a list for my model, and iterate through that list. However, I must not only iterate through the model, but each item in the list in the model will actually have lists also, so I need to iterate yet again, but obviously I cannot use the same technique, since I'm already using my model for one iteration..

@model List<MVC_MONGODB.Models.PATIENT.Encounter>

<div style="background-color: #00BCD4; width:100%; padding:10px ">
@{
    ViewData["Title"] = "Encounters";
}

<h2>Encounters </h2><hr />

@foreach (var item in Model)
{
    <label>@item.Date</label><br /><br />
    <br /><strong>Chief Complaints/HPI</strong>
    <label>@item.ChiefComplaints</label><br /><br />
    <hr />
}
</div>       

From the above, I have a List, and for each Encounter, I must display a List which is a Encounter.ChiefComplaints

Upvotes: 0

Views: 106

Answers (1)

Backs
Backs

Reputation: 24903

Just add one more cycle:

@model List<MVC_MONGODB.Models.PATIENT.Encounter>

<div style="background-color: #00BCD4; width:100%; padding:10px ">

@{
    ViewData["Title"] = "Encounters";
}

<h2>Encounters </h2><hr />

@foreach (var item in Model)
{
    <label>@item.Date</label><br /><br />
    <br /><strong>Chief Complaints/HPI</strong>
    <label>@item.ChiefComplaints</label><br /><br />

    @foreach(var chief in item.ChiefComplaints)
    {
        //do smth with chief
    }
}

</div>   

Upvotes: 3

Related Questions