我是正确播种数据还是做错了什么?

时间:2016-11-04 17:14:07

标签: c# html .net database configuration

我正在尝试显示另一个对象中的对象的值。我已经重写了好几天,并用谷歌搜索它,试图找出我出错的地方。我从一个更复杂的项目开始,但现在我写了一个非常简单的学校作为一个例子,并尝试不同的东西。我根据互联网上的例子至少以30种不同的方式改写了配置,到目前为止还没有任何工作。任何帮助我解开的人都会非常感激!

类:

public class Teacher
{
    public int teacherID { get; set; }
    public string name { get; set; }
}

public class Course
{
    [Key]
    public int ID { get; set; }
    public string courseName { get; set; }
    public int? teacherID { get; set; }
    public Teacher teacher { get; set; }
}

public class schoolDBContext : DbContext
{
    public DbSet<Course> Courses { get; set; }
    public DbSet<Teacher> Teachers { get; set; }
}

配置:

protected override void Seed(school.Models.schoolDBContext context)
        {

        context.Courses.AddOrUpdate(i => i.ID,
            new Course
            {
                courseName = "Math",
                teacher = new Teacher { teacherID = 1, name = "Ms. Brooks" },
                teacherID = 1
            },
             new Course
            {
                courseName = "Science",
                teacher = new Teacher { teacherID = 2, name = "Ms. James" },
                teacherID = 2
            },
            new Course
            {
                courseName = "History",
                teacher = new Teacher { teacherID = 3, name = "Ms. Davidson"},
                teacherID = 3
            }
       );
    }
}

查看:

@model IEnumerable<school.Models.Course>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.courseName)
        </th>
        <th>
        @Html.DisplayNameFor(model => model.teacher.name)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.courseName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.teacher.name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

What the view looks like

如何在课程视图中显示教师姓名?

如果我在控制器中执行此操作,一切正常。

 public ActionResult Index()
    {
        var _coursesList = new List<Course>();
        _coursesList.Add(new Course { ID = 1, courseName = "English", teacherID = 1, teacher = new Teacher { teacherID = 1, name = "Mrs. Thomas" } });
        _coursesList.Add(new Course { ID = 2, courseName = "History", teacherID = 2, teacher = new Teacher { teacherID = 2, name = "Ms. Jones" } });
        _coursesList.Add(new Course { ID = 3, courseName = "Math", teacherID = 2, teacher = new Teacher { teacherID = 2, name = "Ms. Fisher" } });

        return View(_coursesList);

       // return View(db.Courses.ToList());
    }

Code Working

所以它必须是我播种问题数据的方式,对吗?

1 个答案:

答案 0 :(得分:0)

这些行:

<td>
        @Html.DisplayFor(modelItem => item.courseName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.teacher.name)
    </td>

在lambda:

DisplayFor(modelItem => item.teacher.name)

看起来你正在推出错误的物品。

虽然根据您创建迭代的方式,为什么不使用:

@item.teacher.name

而不是:

@Html.DisplayFor(modelItem => item.teacher.name)