locked
Ienumerator error - any inputs will be helpful RRS feed

  • Question

  • User-945256609 posted

    I am getting the following error. I am learning from the videos/usage code from internet.

    Severity Code Description Project File Line Suppression State
    Error CS1579 foreach statement cannot operate on variables of type 'HomeDetailsViewModel' because 'HomeDetailsViewModel' does not contain a public instance definition for 'GetEnumerator' EmployeeManagement C:\projects\EmployeeManagement\Views\Home\Details.cshtml 17 Active

    Details.cshtml

    @model EmployeeManagement.ViewModels.HomeDetailsViewModel

    @{ ViewBag.Title = "Employee Details";}
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    </head>
    <body>
    <table><thead>
    <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Department</th>
    </tr>
    </thead>
    <tbody>
    @foreach(var employee in Model)

    <tr>



    <td>@employee.Id</td>
    <td>@employee.Name</td>
    <td>@employee.Department</td>
    </tr>
    }</tbody> </table></body></html>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;

    namespace EmployeeManagement.Models
    {
    public interface IEmployeeRepository
    {
    Employee GetEmployee(int Id);
    IEnumerable<Employee> GetEmployees();
    }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;

    namespace EmployeeManagement.Models
    {
    public class Employee
    {
    public int Id { get; set; }
    public string Name { get; set; }

    public string Email { get; set; }

    public string Department { get; set; }

    }
    }

    using EmployeeManagement.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;

    namespace EmployeeManagement.ViewModels
    {
    public class HomeDetailsViewModel
    {
    public Employee Employee { get; set; }
    public string PageTitle { get; set; }
    }
    }

    Saturday, May 9, 2020 12:51 PM

All replies

  • User-474980206 posted

    You model is not a collection. So there is nothing to loop over,

    .

    Saturday, May 9, 2020 3:22 PM
  • User-945256609 posted

    many thanks for your reply.

    Saturday, May 9, 2020 5:31 PM