User521171331 posted
Hi,
This is my Razor page code.
@model IEnumerable<ContosoUniversity.Models.Attendance>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.EmployeeID)
</th>
<th>
@Html.DisplayNameFor(model => model.WorkDate)
</th>
<th>
@Html.DisplayNameFor(model => model.WorkHour)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.WorkDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.WorkHour)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
Now, it is showing the foreign key employee ID. But I need to show employee name instead. In my controller, my ActionResult Index method code is below but not working.
public ActionResult Index(int? SelectedEmployee)
{
var employees = db.Employees.OrderBy(q => q.Name).ToList();
ViewBag.SelectedEmployee = new SelectList(employees, "EmployeeID", "Name", SelectedEmployee);
int employeeID = SelectedEmployee.GetValueOrDefault();
IQueryable<Attendance> attendances = db.Attendances
.Where(c => !SelectedEmployee.HasValue || c.EmployeeID == employeeID)
.OrderBy(d => d.Id)
.Include(d => d.Employee);
return View(attendances.ToList());
}