ASP.NET MVC - 显示它们都有一个项目列表项的列表 [英] ASP.NET MVC - Displaying a list of items which each have a list of items

查看:99
本文介绍了ASP.NET MVC - 显示它们都有一个项目列表项的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这是解释这个的最好方法......

我有3个视图对象:学校,课程和课程。每个学校都有多个课程,每门课程可以有多个类(认为当然作为学习计划,用类是实际类)。在我的主要看法,我显示所有的学校,然后按一来一去的。在那个CourseView页面,它显示了学校的名称,以及与学校相关的所有课程。我试图做的,也有还列出每门课程相关的所有课程。这是可能的(没有一吨的jQuery / JSON巫术的,这我仍然在学习)?

 公共类学校
{
    公众诠释标识{搞定;组; }
    公共字符串SchoolName {搞定;组; }
    公共字符串WelcomeMsg {搞定;组; }
    公共字符串SchoolLogo {搞定;组; }
    公开名单<课程和GT;课程{搞定;组; }
}公共类课程
{
    公众诠释标识{搞定;组; }
    公共字符串课程code {搞定;组; }
    公共字符串课程名{获得;组; }
    公众诠释SchoolId {搞定;组; }
    公开名单<班级> {类​​搞定;组; }
}公共类类
{
    公众诠释标识{搞定;组; }
    公共字符串ClassNumer {搞定;组; }
    公众诠释CourseId {搞定;组; }
    公众诠释InstructorId {搞定;组; }
}

在我SchoolDAO.cs,我有以下几点:

  {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue(@ SchoolId,SchoolId);
                    使用(SqlDataReader的博士= cmd.ExecuteReader())
                    {
                        如果(dr.Read())
                        {
                            //在另一种方法填补学校的对象。
                            学校= readRecord(DR);
                        }
                    }                    //获取可用的这所学校里的课程。
                    school.Courses = CoursesDAO.GetCourses(SchoolId);                    回到学校;
                }

这也要求在CourseDAO.cs一个方法来获得可在该学校的课程:

  {
                CMD的SqlCommand =新的SqlCommand(Courses.GetCourses,康恩);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue(@ SchoolId,SchoolId);
                使用(SqlDataReader的博士= cmd.ExecuteReader())
                {
                    而(dr.Read())
                    {
                        courses.Add(readRecord(DR));
                    }
                }                //获取可用的每门课程的所有课程。
                的foreach(在课程VAR课程)
                {
                    course.Classes = ClassDAO.GetClasses(course.Id);
                }                返回课程;
            }

然后调用ClassDAO方法来获取每门课程的所有类:

  {
                CMD的SqlCommand =新的SqlCommand(Courses.GetCourses,康恩);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue(@ SchoolId,courseId);
                使用(SqlDataReader的博士= cmd.ExecuteReader())
                {
                    而(dr.Read())
                    {
                        classes.Add(readRecord(DR));
                    }
                }                返回班;
            }

所以最后,我的问题是:如何显示的学校,课程,并在View类的列表,从Model

我查看具有以下,显示我在看哪个学校,以及课程的学校名单:

 < D​​IV CLASS =显示字段>
    < H1><%:Model.SchoolName%GT;< / H1>
    < H3><%:Model.WelcomeTitle%GT;< / H3 GT&;
    <&BLOCKQUOTE GT; < I><%:Model.WelcomeText%GT;< / I>< / BLOCKQUOTE>
< / DIV><! - 课程 - >
< D​​IV CLASS =显示字段>
    <表ID =课程>
            <&THEAD GT;
                <第i场code< /第i
                <第i个课程名称< /第i
            < / THEAD>
        <&TBODY GT;
        <%
            的foreach(在Model.Courses VAR项){
        %GT;
            &所述; TR>
                < TD><%:Html.DisplayFor(型号=> item.Course code)%GT;< / TD>
                < TD><%:Html.DisplayFor(型号=> item.CourseName)%GT;< / TD>
            < / TR>
        <%}%GT;< / TBODY>
    < /表>
< / DIV>

这表后,我希望把另一个表id为类,但可悲的是,当我尝试做:

 <%
            的foreach(在Model.Courses VAR类){
        %GT;
            &所述; TR>
                < TD><%:Html.DisplayFor(型号=> item.ClassNumber)%GT;< / TD>
            < / TR>
        <%}%GT;

这是行不通的 - 我假设,因为我要么不知道如何访问属于清单列表,或因为这不是模型的工作方式

任何洞察到这将是极大的pciated AP $ P $。谢谢!

PS - >业务 - - 该信息是从DAO传递>通过简单的方法如控制器:

SchoolController:

 公众的ActionResult ViewSchool(INT标识)
    {
        校舍= SchoolBusiness.GetSchool(ID);
        返回查看(学校);
    }

SchoolBusiness.GetSchool:

 公共静态学校GetSchool(INT标识)
    {
        校舍= SchoolDAO.GetSchool(ID);
        回到学校;
    }


解决方案

您需要引用每门课程,你通过它迭代,并得到类的列表。要做到这一点,巢的foreach 语句。

我改变了HTML是简单的名单,因为我觉得它可以更容易理解。它应该是足够简单更改为嵌套表,如果这是你想要的。

 <! - 课程 - >
< UL>
    <%的foreach(在Model.Courses VAR课程){%GT;
        <立GT;
            <%:Html.DisplayFor(型号=> course.Course code)%GT;
            <%:Html.DisplayFor(型号=> course.CourseName)%GT;
            <! - 类 - >
            < UL>
            <%的foreach(在course.Classes VAR类){%GT;
                <立GT;<%:Html.DisplayFor(型号=> class.ClassNumber)%GT;< /李>
            <%}%GT;
            < / UL>
        < /李>
    <%}%GT;
< / UL>

I hope this is the best way of explaining this...

I have 3 view objects: School, Courses, and Classes. Each school has multiple courses, and each course can have multiple classes (think of a course as a program of study, with classes being the actual classes). In my main View, I display all schools, and click one to go to it. On that "CourseView" page, it displays the name of the school as well as all courses associated with that school. What I'm trying to do, is also have all classes associated with each course listed also. Is this possible (without a ton of jQuery/JSON wizardry, which I am still learning)?

public class School
{
    public int Id { get; set; }
    public string SchoolName { get; set; }
    public string WelcomeMsg { get; set; }
    public string SchoolLogo { get; set; }
    public List<Course> Courses { get; set; }
}

public class Course
{
    public int Id { get; set; }
    public string CourseCode { get; set; }
    public string CourseName { get; set; }
    public int SchoolId { get; set; }
    public List<Class> Classes { get; set; }
}

public class Class
{
    public int Id { get; set; }
    public string ClassNumer { get; set; }
    public int CourseId { get; set; }
    public int InstructorId { get; set; }
}

In my SchoolDAO.cs, I have the following:

                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@SchoolId", SchoolId);
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            // fill school object in another method.
                            school = readRecord(dr);
                        }
                    }

                    // Get all courses available for this school.
                    school.Courses = CoursesDAO.GetCourses(SchoolId);

                    return school;
                }

Which also calls a method in the CourseDAO.cs to get the courses available at that school:

            {
                SqlCommand cmd = new SqlCommand("Courses.GetCourses", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@SchoolId", SchoolId);
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        courses.Add(readRecord(dr));
                    }
                }

                // Get all classes available for each course.
                foreach (var course in courses)
                {
                    course.Classes = ClassDAO.GetClasses(course.Id);
                }

                return courses;
            }

Which then calls the ClassDAO method to get all classes for each course:

            {
                SqlCommand cmd = new SqlCommand("Courses.GetCourses", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@SchoolId", courseId);
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        classes.Add(readRecord(dr));
                    }
                }

                return classes;
            }

So finally, my question is this: How do I display the list of schools, courses, and classes in a View, from the Model?

My View has the following, which displays which school I'm looking at, and the list of courses for that school:

<div class="display-field">
    <h1><%: Model.SchoolName %></h1>
    <h3><%: Model.WelcomeTitle %></h3>
    <blockquote> <i><%: Model.WelcomeText %></i></blockquote> 
</div>

<!-- Courses -->
<div class="display-field">
    <table id="courses">             
            <thead>
                <th>Course Code</th>
                <th>Course Name</th>
            </thead>
        <tbody>
        <%
            foreach (var item in Model.Courses) {
        %>
            <tr>
                <td><%: Html.DisplayFor(model => item.CourseCode) %></td>
                <td><%: Html.DisplayFor(model => item.CourseName) %></td>
            </tr>
        <% } %></tbody>
    </table>
</div>

After that table, I wanted to put another table with id="classes", but sadly when I try to do:

        <%
            foreach (var class in Model.Courses) {
        %>
            <tr>
                <td><%: Html.DisplayFor(model => item.ClassNumber) %></td>
            </tr>
        <% } %>

It won't work - I'm assuming because I either don't know how to access a list belonging to a list, or because that's not the way the Model works.

Any insight into this would be appreciated greatly. Thanks!

PS - The information is passed from the DAO -> Business -> Controller by simple methods such as:

SchoolController:

    public ActionResult ViewSchool(int Id)
    {
        School school = SchoolBusiness.GetSchool(Id);
        return View(school);
    }

SchoolBusiness.GetSchool:

    public static School GetSchool(int Id)
    {
        School school = SchoolDAO.GetSchool(Id);
        return school;
    }

解决方案

You need to reference each course as you iterate through it and get the list of classes. To do this, nest the foreach statements.

I changed the HTML to be simple lists because I think it makes it easier to understand. It should be simple enough to change to nested tables if that is what you want.

<!-- Courses -->
<ul>
    <% foreach (var course in Model.Courses) { %>
        <li>
            <%: Html.DisplayFor(model => course.CourseCode) %>
            <%: Html.DisplayFor(model => course.CourseName) %>
            <!-- Classes -->
            <ul>
            <% foreach (var class in course.Classes) { %>
                <li><%: Html.DisplayFor(model => class.ClassNumber) %></li>
            <% } %>
            </ul>
        </li>
    <% } %>
</ul>

这篇关于ASP.NET MVC - 显示它们都有一个项目列表项的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆