如何显示对象内对象字段的值?

时间:2016-11-03 23:19:04

标签: c# html .net

我真的不知道用正确的词来提出我需要的问题,这意味着我并没有从谷歌那里得到任何东西,所以我只想创建一个简单的例子。如果你能回答我的问题,或者只是指出我需要搜索的方向,那就太棒了。

说我有这些课程:

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

如何在显示课程时以HTML格式显示教师姓名。例如,当我在视图中有以下内容时,教师姓名为空白。我需要更改什么才能显示教师的课程名称?

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.teacher.Name)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.teacher.Name)
        </td>
   </tr>
}

这是我重写了30次的配置:

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

页面看起来像这样。 enter image description here

但如果我为控制器执行此操作:

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

我明白了:

enter image description here

2 个答案:

答案 0 :(得分:1)

确保填写模型对象

前:

 public ActionResult Index()
    {
        var _courselst = new List<Course>();
        _courselst.Add(new Course { CourseID = 1, Name = "IT", Teacher = new Teacher { Name = "Scot", TeacherID = 1 } });
        _courselst.Add(new Course { CourseID = 2, Name = "ACC", Teacher = new Teacher { Name = "Manj", TeacherID = 2 } });
        _courselst.Add(new Course { CourseID = 3, Name = "MATHS", Teacher = new Teacher { Name = "Demion", TeacherID = 3 } });

        return View(_courselst);
    }

答案 1 :(得分:0)

我不确定我理解你的问题,但我认为你可以创建一个只读属性,如:

public class Course
{
    public int CourseID { get; set; }
    public String Name { get; set; }
    public Teacher Teacher { get; set; }
    public String TeacherName 
    { 
        get {
            return Teacher?.Name ?? Name
        }
    }
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TeacherName)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Teacher.Name)
        </td>
   </tr>
}