Reputation: 146
I use in my View in MVC this to display table:
@foreach (var use in Model)
{
<tbody>
<tr>
<td>@Html.DisplayFor(modelItem => use.userID)</td>
<td>@Html.DisplayFor(modelItem => use.username)</td>
<td>@Html.DisplayFor(modelItem => use.Roles)</td>
<tr>
<tbody>
Now column "Roles" is display as number (because it is set as integer in my userTable). But I also have table roleTable, where I have Id and NameOfRole. Now I whant to display NameOfRole from roleTable instead use.Roles.
So far I have try this code:
<td>
@{
var test = from c in new userDbEntities().roleTables
where c.Id == use.Roles
select c.NameOfRole;
Html.DisplayFor(modelItem => test);
}
</td>
But this is not working.
Any Ideas where is a mistake?
Thanks for help...
Upvotes: 0
Views: 918
Reputation: 5121
Assuming Roles is actually navigation property then you should be able to use
<td>@String.Join(", ", use.Roles.Select(r => r.NameOfRole))</td>
To get all roles as comma separated list.
If you dont include the roles when getting your model, they will be lazy loaded when referred.
OR
If Roles is not a navigation property then you can use
<td>
@String.Join(", ", new userDbEntities()
.roleTables.Where(rt => rt.Id == use.Roles).Select(r => r.NameOfRole))
</td>
But you should not be getting any data from repository/db when you are building your view, your Model should already contain everything you need to display.
Upvotes: 1