I have two tables: Users (user_id, name) and Records (record_id, record_time, user1_id, user2_id). So for each record there should be two users. I want to list records and display user names of user 1 and user 2. Now default View structure can display only one related user name by id.
How do I retrieve the name of second user of the record?
INDEX action
public ActionResult Index()
{
var records = db.Records.Include(r => r.User);
return View(records.ToList());
}
INDEX view
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.record_time)
</th>
<th>
@Html.DisplayNameFor(model => model.User.name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.record_time)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.record_id }) |
@Html.ActionLink("Details", "Details", new { id=item.record_id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.record_id })
</td>
</tr>
}
</table>