MVC视图View向控制器Controller传输List集合数据

控制器代码:

public class DefaultController : Controller
    {
        // GET: Default
        public ActionResult Index()
        {
            MyClass cls = new MyClass()
            {
                ClassID = "cls001",
                ClassName = "三年级(8)班",
                Students = new List
                {
                    new Student{ StuID="stu0001",Name="肖子毅"},
                    new Student{ StuID="stu0002",Name="付诗函"}
                }
            };
            return View(cls);
        }
        public ActionResult Edit(MyClass cls)
        {

            return View();
        }

视图代码:

 

@{
    Layout = null;
}

@model WebApplication1.Models.MyClass




    
    Index


    

班级id: 班级名称:

@for (int i = 0; i < Model.Students.Count; i++) {

学生ID: 学生姓名:

}

实体类定义:

public class MyClass
    {
        public String ClassID { get; set; }
        public String ClassName { get; set; }
        public List Students { get; set; }
    }
    public class Student
    {
        public String  StuID { get; set; }
        public String Name { get; set; }
    }

结果展示:

MVC视图View向控制器Controller传输List集合数据_第1张图片MVC视图View向控制器Controller传输List集合数据_第2张图片

你可能感兴趣的:(C#/ASP.NET/VS)