首页 > 解决方案 > 在 mvc 中,当控制器类正确发送列表时,视图没有接收到列表

问题描述

我正在从控制器发送一个列表到视图,但视图没有收到它,尽管控制器中填充了列表

控制器:

public class DepartmentController : Controller
{
    // GET: Department
    public ActionResult Index() {
        EmployeeContext employeeContext = new EmployeeContext();
        List<Department> departments = 
        employeeContext.Departments.ToList();
        return View();
    }
}

看法:

@model  IEnumerable<MVCDemo.Models.Department>
@using MVCDemo.Models;

@{
    ViewBag.Title = "Departments List";
}
<div style="font-family:Arial">
<h2>Departments List</h2>
<ul>
    @foreach (Department Department in  @model) {
        <li>@Html.ActionLink(Department.name, "Index","Employee", new { 
        departmentid = Department.ID })</li>
    }
</ul>
</div>

标签: asp.net-mvc-4

解决方案


public class DepartmentController : Controller{
// GET: Department
public ActionResult Index() {
    EmployeeContext employeeContext = new EmployeeContext();
    List<Department> departments = 
    employeeContext.Departments.ToList();
    return View(departments);
}}

推荐阅读