不确定如何使用MVC列出所有记录

时间:2017-12-28 20:56:34

标签: c# asp.net-mvc asp.net-mvc-5

我还是MVC的新手。我试图在Employees表中显示所有员工。我创建了一个EmployeeView模型,然后创建了一个具有IEnumerable EmployeeView的EmployeesView模型。在Employee Manager中,我获得所有员工并将其作为IEnumerable返回。在控制器中,我将该列表放入模型中。我基于EmployeesView模型创建了ViewEmployees。

每当我尝试遍历员工时,我必须创建一个新的foreach循环,我不确定为什么。另外,运行应用程序时出现此错误:

  

传递到字典中的模型项是类型的   ' Project_Tracking_MVC_Training.Models.ViewModel.EmployeesView',但   这本词典需要一个类型的模型项   ' System.Collections.Generic.IEnumerable`1 [Project_Tracking_MVC_Training.Models.ViewModel.EmployeesView]'

我不确定如何解决这个问题。

EmployeeModel.cs:

namespace Project_Tracking_MVC_Training.Models.ViewModel
{
    public class EmployeeView
    {
        [Key]
        public int PK_EmployeeID { get; set; }

        [Required]
        [Display(Name = "Employee Type")]
        public int FK_EmployeeTypeID { get; set; }

        [Required]
        [Display(Name = "Employee First Name")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Employee Last Name")]
        public string LastName { get; set; }

        [Required]
        [Display(Name = "Is Active")]
        public bool IsActive { get; set; }
    }


    public class EmployeesView
    {
        public IEnumerable<EmployeeView> Employees {get; set;}
    }
}

EmployeeManager.cs:

namespace Project_Tracking_MVC_Training.Models.EntityManager
{
    public class EmployeeManager
    {

        public static IEnumerable<EmployeeView> GetAllEmployees()
        {
            using (ProjectTrackingEntities1 db = new ProjectTrackingEntities1())
            {
                var emp = db.Employees.Select(o => new EmployeeView
                {
                    PK_EmployeeID = o.PK_EmployeeID,
                    FK_EmployeeTypeID = o.FK_EmployeeTypeID,
                    FirstName = o.FirstName,
                    LastName = o.LastName,
                    IsActive = o.IsActive
                }).ToList();

                return emp;
            }
        }
    }
}

EmployeeController.cs

namespace Project_Tracking_MVC_Training.Controllers
{
    public class EmployeeController : Controller
    {
        public ActionResult ViewEmployees()
        {
            EmployeesView model = new EmployeesView();

            model.Employees = EmployeeManager.GetAllEmployees();
            return View(model);
        }
    }
}

ViewEmployees.cshtml:

@model IEnumerable<Project_Tracking_MVC_Training.Models.ViewModel.EmployeesView>

@{
    ViewBag.Title = "ViewEmployees";
}

<h2>ViewEmployees</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>Employee ID</th>
        <th>Actions</th>
    </tr>

@foreach (var item in Model) {
    foreach (var emp in item.Employees)
    {
        <tr>
            <td>@emp.PK_EmployeeID</td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>

    }
}

</table>

0 个答案:

没有答案