1

I have created a view model EmployeeViewModel from two other models Employee and HolidayRequestForm as I need to use them in the one view. I'm having a problem with my view displaying data from my models. Here are 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;}

}

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; }
}

Employee View Model:

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

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

Here is my 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);


    }

I am trying to implement this model onto my view but I'm having trouble. Here is the part of the view causing troube:

@model LotusWorksHT.Models.EmployeeViewModel

<div style=" position:relative;top:40px; border-radius: 0px; border-color: #F47B20; border-style: solid; border-width: 5px; background-repeat: no-repeat; background-position: right; padding: 5px; background-size: contain; background-color:white ">
        <h2 align="center">Holiday Requests Record</h2>
        <table class="table table-striped">

            <tr>
                <th>
                    Start Date
                </th>

                <th>
                    Finish Date
                </th>
                <th>
                    HoursTaken
                </th>
                <th>
                    Comments
                </th>
                <th>
                    Year Created
                </th>

                <th>
                    Month Created
                </th>
                <th>
                    Day Created
                </th>
                <th>
                    Year Of Holiday
                </th>
            </tr>

  @foreach (var item in Model) {
<tr>

                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.StartDate)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.FinishDate)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.HoursTaken)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.Comments)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.YearCreated)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.MonthCreated)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.DayCreated)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.YearOfHoliday)
                </td>
            </tr>

   }
        </table>

        </div>
9
  • What error are you getting? Commented Jan 16, 2019 at 15:19
  • You're setting your holidayRequestForm in your Details function to a List<HolidayRequestForm> , but the HolidayRequestForm inside the EmployeeViewModel is of type HolidayRequestForm so not a list. And also , if you want to loop through a list of holidayRequestForms in your view , you will need to loop through Model.HolidayRequestForms as its a list and your model is just based on an Instance of EmployeeViewModel Commented Jan 16, 2019 at 15:20
  • @arod " foreach statement cannot operate on variables of type 'LotusWorksHT.Models.EmployeeViewModel' because 'LotusWorksHT.Models.EmployeeViewModel' does not contain a public definition for 'GetEnumerator'" Commented Jan 16, 2019 at 15:24
  • @Alica Sorry, that was old code, let me fix that! Commented Jan 16, 2019 at 15:28
  • Did you try changing it to : @foreach (var item in Model.HolidayRequestForm) as that is the list you want to loop through Commented Jan 16, 2019 at 15:32

1 Answer 1

1

Try changing :

@foreach (var item in Model)

to

@foreach (var item in Model.HolidayRequestForm)

As its the HolidayRequestForm List you want to loop through here . And then try changing

model.HolidayRequestForm.StartDate

to

item.StartDate or model => model.StartDate

To get at each property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.