dmce
dmce

Reputation: 222

MVC + Entities Framework: Many to Many relationships

Im new to MVC and EF. I have a basic app that lists info from a few (4) tables relating to some servers. One of the relationships is a many to many (Servers to Roles).

I pass my ViewData to the View by making use of this in the controller:

ViewData.Model = (from s in _db.Servers.Include("Locations").Include("OperatingSystems").Include("Roles")
                          select s).ToList();

However im not sure how to go about displaying the multiple roles for each server in my View?

Upvotes: 1

Views: 1164

Answers (1)

Rokey Ge
Rokey Ge

Reputation: 681

Suppose your ViewData.Model works and the Model is of List type.

<% foreach (var server in Model)
{%>
    <% foreach (var role in server.Roles)
    {%>
    <li><%= role.Name %></li>
    <%}%>
<%}%>

Upvotes: 2

Related Questions