ASP.NET Display more than one entry/row

Viewed 37

I'm quite new to ASP.NET MVC. I am creating a holiday tracking app, and I'm attempting to create a details page for each Employee. I want this page to display a table showing a table of all the holiday request that employee has taken. I've gotten it to work but it only shows only one row.

Here's my models

public partial class Employee
{
    public int EmployeeID { get; set; }
    public string FullName { get; set; }
    public string EmailID { get; set; }
    public string Password { get; set; }
    public System.DateTime StartDate { get; set; }
    public int RoleID { get; set; }
    public int ShiftID { get; set; }
    public int AreaID { get; set; }
    public int DisciplineID { get; set; }
    public int SiteID { get; set; }
    public int ALCategory { get; set; }
    public Nullable<int> HoursTaken { get; set; }
    public Nullable<int> AwardedLeave { get; set; }
    public Nullable<int> TotalHoursThisYear { get; set; }
    public int HoursCarriedForward { get; set; }
    public Nullable<int> EntitlementRemainingThisYear { get; set; }
    public string Comments { get; set;}

}

Employee Model:

 public class EmployeeViewModel
{
    public Employee Employee { get; set; }

    public HolidayRequestForm HolidayRequestForm { get; set; }
}

Holiday Request Form Model:

 public partial class HolidayRequestForm
{
    public int RequestID { get; set; }
    public int EmployeeID { get; set; }
    public System.DateTime StartDate { get; set; }
    public System.DateTime FinishDate { get; set; }
    public int HoursTaken { get; set; }
    public string Comments { get; set; }
    public int YearCreated { get; set; }
    public int MonthCreated { get; set; }
    public Nullable<int> DayCreated { get; set; }
    public Nullable<int> YearOfHoliday { get; set; }

    public virtual Employee Employee { get; set; }
}

Controller Action:

 public ActionResult Details(int? id)
      {
        Employee employee =  db.Employees.FirstOrDefault(emp => emp.EmployeeID == id);
        HolidayRequestForm holidayRequestForm db.HolidayRequestForms.FirstOrDefault(emp => emp.EmployeeID == id);



        EmployeeViewModel employeeViewModel = new EmployeeViewModel()
           {
               Employee = employee,
               HolidayRequestForm = holidayRequestForm,

           };

        return View(employeeViewModel);


    }

My View is then just a HTML table to display the data from the database. So the question is how do I display more than one row or entry?? Is it that I'm using FirstorDefault?

1 Answers

You must change Employee Model and declare a list for the Holiday :

Employee Model:

public class EmployeeViewModel
{
    public Employee Employee { get; set; }

    public List<HolidayRequestForm> HolidayRequestForm { get; set; }
}

Controller Action:

public ActionResult Details(int? id)
{
        Employee employee =  db.Employees.FirstOrDefault(emp => emp.EmployeeID == id);
        List<HolidayRequestForm> holidayRequestForm = db.HolidayRequestForms.Where(emp => emp.EmployeeID == id).ToList();



        EmployeeViewModel employeeViewModel = new EmployeeViewModel()
           {
               Employee = employee,
               HolidayRequestForm = holidayRequestForm,

           };

        return View(employeeViewModel);


}
Related