ab1428x
ab1428x

Reputation: 804

Linq refer to list of objects which were passed with an include statement

Here is how I pass a model to a view:

        public ActionResult Public()
    {
        db = new Entities();

        var regions = db.Regions.Include(d => d.Days);

        return View(regions.ToList());
    }

Now I want to be able to create a foreach in the view which will access the collection of days I included in regions for that particular region. Is there a way to do this through link or should I create a seperate ViewModel that contains fields separately for Regions and for Days?

part of view code:

...       
  @for(int i = 1; i < Model.Count(); i ++){
                <div id="_page_@i" class="frame" style="display: block;">
                    <p>
                        <button  style="width:70px" type="button">Add +</button>
                    </p>

                    <p>
            <table class="table striped bordered hovered" >
        <thead>
            <tr>
                <th class="text-left">Title</th>
                <th class="text-left">Date</th>
                <th class="text-left"> </th>
            </tr>
        </thead>
        <tbody>

            @foreach(var item in Model){
            <tr class="">
                <td>@item</td>
                <td class="right"></td>
                <td class="right" style="width: 180px; height: 35px"><button  style="width:159px; height: 32px" type="button">Delete -</button></td>
              </tr>
            }

        </tbody>

    </table>
            </p>
                </div>
                }
            </div>

        </div>

    </div>

        </div>

I need the foreach to refer to the Days in the Regions so that item would be one Day.

Upvotes: 0

Views: 47

Answers (1)

Romias
Romias

Reputation: 14133

In your view you do a foreach within a foreach. In my example, it will creale a list of Days for all Regions... assuming the Day has a property "Name":

<ul>
@foreach( var r in Model){
    foreach( var d in r.Days){
         <li>d.Name</li>
    }
}
</ul>

Upvotes: 3

Related Questions