ASP MVC 嵌套模型

ASP MVC Nested Models

我需要一个显示 header-detail 行信息的视图。对于一个简单的案例,我设置了一个 Course-Student 微型数据库,其中包含 4 个简单的 table。课程和学生 table 链接到一个部分 table,其中也有一个学生成绩。此外,学生 table 属于学生类型 Table。有帮助程序 classes 保存课程的 "header" 数据列表和每个参加该 class 的学生的 "detail" 列表。

namespace SchoolA.Models
{
public class CourseInfo2
{
    public int CourseID { get; set; }
    public int CourseNumber { get; set; }
    public string CourseName { get; set; }
    public IEnumerable<CourseSectionList> CourseStudentList { get; set; }
}
}
namespace SchoolA.Models
{
public class CourseSectionList
{
    public string Student { get; set; }
    public string Type { get; set; }
    public string Grade { get; set; }
}
}

控制器是:

public ActionResult Courselisting()
    {
        List<CourseInfo2> courseInfo2 = new List<CourseInfo2>();
        List<CourseSectionList> courseSectionList = new List<CourseSectionList>();
        var Cquery = from c in db.Courses select c;
        foreach (var item in Cquery)
        {
            Course course = db.Courses.Find(item.CourseID);   // first find the selected course
            // get the sections
                        foreach (var s in query)  // go through each section
                        {
                           // get the section data
                            courseSectionList.Add(new CourseSectionList
                            {
                                Student = student.StudentName,
                                Type = studentType.StudentTypeName,
                                Grade = s.SectionStudentGrade        
                            });
                        }  // end of section loop
            courseInfo2.Add(new CourseInfo2
            {
                CourseID = course.CourseID,
                CourseNumber = course.CourseNumber,
                CourseName = course.CourseName,
                CourseStudentList = courseSectionList
            });
        }   // end of Course loop
        return View(courseInfo2);    // Course List and Section list for each course
    }

视图是:

    @model SchoolA.Models.CourseInfo2                            
@*  doesn't work either: model List<SchoolA.Models.CourseInfo2>*@
<div>
     <table>
        <tr><th>ID</th> <th>Number</th> <th>Course</th></tr>
        @foreach (var CourseInfo2 in Model)
        {
            <tr>
                <td>
                    @CourseInfo2.CourseID
                </td>
                <td>
                    @CourseInfo2.CourseNumber
                </td>
                <td>
                    @CourseInfo2.CourseName

                </td>
                <td>
                    <table class="table">
                        <tr><th>Student</th> <th>Type</th><th>Grade</th></tr>
                        @foreach (var s in Model.CourseStudentList)
                        {
                            <tr>
                                <td>
                                    @s.Student
                                </td>
                                <td>
                                    @s.Type
                                </td>
                                <td>
                                    @s.Grade
                                </td>
                            </tr>
                        }
                    </table>
                </td>
            </tr>
        }
    </table>
</div>

问题是我在尝试将这两个模型传递给视图时遇到了各种错误。在如上所示的代码中,我收到此错误:

CS1579: foreach statement cannot operate on variables of  type 'SchoolA.Models.CourseInfo2' because 'SchoolA.Models.CourseInfo2' does not contain a public definition for 'GetEnumerator'

我尝试了多种传递模型的变体,但总是 运行 出现一个错误,另一个错误阻止两个模型在视图中工作。我应该注意到,我独立测试了视图的每个部分,并且控制器代码工作正常,可以将正确的数据传递给视图。但是,我不能将两者结合起来。

问题似乎出在我创建模型实例的方式以及将它们传递给视图的方式。我做错了什么?

您正在传递视图 List<SchoolA.Models.CourseInfo2>,但视图需要一个 SchoolA.Models.CourseInfo2

将您的@model 声明更改为IEnumerable<SchoolA.Models.CourseInfo2>

然后更改您的 foreach 循环。

将第一个循环更改为 foreach (var courseInfo in Model)

将内循环改为foreach (var s in courseInfo.CourseStudentList)

您传递的模型是对象列表,而在视图中您有单个对象

更改您的视图模型绑定如下:

@model List<SchoolA.Models.CourseInfo2>

问题:我必须编辑 Level2Name(通过 UI 它是子网格内的一个文本框)。

我能够编辑 Level1name(父网格文本字段)并能够在我的控制器中获取值。

问题:如何编辑嵌套的 textbox

我试过的。

代码:

型号

public class Level0
        {
            public Level0()
            {
                Level1= new List<Level1>();                
            }
            public int? ID{ get; set; }
            public int? NameText{ get; set; }           
            public List<Level1> lev1{ get; set; }
        }

        public class Level1
        {
            public Level1()
            {
                Level2= new List<Level2>();
            }
            public int  Lev1ID  {get;set;}
            public string Level1name{ get; set; }
            public List<Level2> level2{ get; set; }
        }    
        public class Level2
        {
            public string Level2_ID { get; set; }
            public string Level2Name{ get; set; }
        }

UI代码

@for (int i = 0; i < Model.Level1.Count; i++)
   {    
     @Html.HiddenFor(modelItem => Model.Floors[i].Lev1ID  ) 

     @for (int j = 0; j < Model.Level1[i].Level2.Count; j++)
        {
  @Html.HiddenFor(m => m.Level1[i].Level2[j].OP)
  @Html.TextBoxFor(m => m.Level1[i].Level2[j].Level2Name, new { @class = "form-control" })
        }
    }