C# ASP.NET MVC - 显示一个项目列表,每个项目都有一个项目列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18603088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC - Displaying a list of items which each have a list of items
提问by SalarianEngineer
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)?
我有 3 个视图对象:学校、课程和班级。每个学校都有多个课程,每个课程可以有多个班级(将课程视为学习计划,班级是实际课程)。在我的主视图中,我显示了所有学校,然后单击一个转到它。在“CourseView”页面上,它显示了学校的名称以及与该学校相关的所有课程。我正在尝试做的是,还列出了与每门课程相关的所有课程。这可能吗(没有大量的 jQuery/JSON 魔法,我还在学习)?
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:
在我的 SchoolDAO.cs 中,我有以下内容:
{
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:
它还调用 CourseDAO.cs 中的方法来获取该学校可用的课程:
{
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:
然后调用 ClassDAO 方法来获取每门课程的所有类:
{
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:
在那张桌子之后,我想放另一个带有 id="classes" 的桌子,但遗憾的是,当我尝试这样做时:
<%
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:
PS - 信息从DAO -> Business -> Controller 通过简单的方法传递,例如:
SchoolController:
学校负责人:
public ActionResult ViewSchool(int Id)
{
School school = SchoolBusiness.GetSchool(Id);
return View(school);
}
SchoolBusiness.GetSchool:
SchoolBusiness.GetSchool:
public static School GetSchool(int Id)
{
School school = SchoolDAO.GetSchool(Id);
return school;
}
采纳答案by PeaceFrog
You need to reference each course as you iterate through it and get the list of classes. To do this, nest the foreach
statements.
您需要在迭代每门课程时引用它并获取课程列表。为此,请嵌套foreach
语句。
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.
我将 HTML 更改为简单列表,因为我认为它更易于理解。如果这是您想要的,它应该足够简单,可以更改为嵌套表。
<!-- 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>
回答by gMailMan
This part doesnt look right:
这部分看起来不对:
foreach (var class in Model.Courses) {
%>
<tr>
<td><%: Html.DisplayFor(model => item.ClassNumber) %></td>
</tr>
<% }
Your Course class doesnt have ClassNumber property.You should say Model.Courses.Classes
,and also put it inside your first loop somewhere so you could access Classes for every course.
Also word class
may cause problems,because its a keyword in c#
你的 Course 类没有 ClassNumber 属性。你应该说Model.Courses.Classes
,并将它放在你的第一个循环中的某个地方,这样你就可以访问每门课程的类。
wordclass
也可能会导致问题,因为它是 c# 中的关键字
回答by Matt Millican
<%
foreach (var item in Model.Courses) {
%>
<tr>
<td><%: Html.DisplayFor(model => item.CourseCode) %></td>
<td><%: Html.DisplayFor(model => item.CourseName) %></td>
</tr>
<tr>
<td>Classes</td>
<td>
<% foreach (var class in item.Classes) { %>
-- list your classes here
<% } %>
</td>
</tr>
<% } %></tbody>
回答by Chris
For a start don't use class as a variable, it's a reserved word, but isn't it something like:
首先不要使用类作为变量,它是一个保留字,但它不是这样的:
<%
foreach (var course in Model.Courses) {
foreach (var courseClass in course.Classes) {
%>
<tr>
<td><%: Html.DisplayFor(model => courseClass.ClassNumber) %></td>
</tr>
<%
}
}
%>