如何在 ASP.NET Core MVC 中使用我的视图模型更新我的 3 个表(我已经有了 Create)

Posted

技术标签:

【中文标题】如何在 ASP.NET Core MVC 中使用我的视图模型更新我的 3 个表(我已经有了 Create)【英文标题】:How can I update my 3 tables using my viewmodel in ASP.NET Core MVC (I already have the Create) 【发布时间】:2021-10-28 16:08:36 【问题描述】:

首先,我有 Parent、Student 和 Current Year Student 表,一个学生需要一个家长,所以显然需要先添加,然后再添加。 我的问题是如何使用 ViewModel 更改 Edit/UpdateStudent Action Result 以编辑数据库中的数据。

这是我的视图模型:

 public class StudentParentViewModel
    

        //parent
        public int ParentID  get; set; 
        public string Title  get; set; 
        public int Contact_Number  get; set; 
        public Nullable<int> Alt_Contact_Number  get; set; 
        public string Email_Address  get; set; 
        public string Home_Address  get; set; 
        public int ParentTypeID  get; set; 
        public string Parent_Name  get; set; 
        public string Parent_Middle_Name  get; set; 
        public string Parent_Surame  get; set; 
        public long PID_Number  get; set; 

        //student
        public int StudentID  get; set; 
        public System.DateTime DoB  get; set; 
        public string Gender  get; set; 
        public bool Remember  get; set; 
        public int Student_TypeID  get; set; 
        public string Student_Name  get; set; 
        public string Student_Middle_Name  get; set; 
        public string Student_Lastname  get; set; 
        public long SID_Number  get; set; 

        //current year student
        public int CurrentYearStudentID  get; set; 
        public Nullable<int> GradeID  get; set; 
        public Nullable<int> ClassID  get; set; 
        public Nullable<int> YearID  get; set; 
    

这是我的创建,用户将上面提到的 3 个实体添加到数据库中的相关表中,所有这些都是一对一的视图。

这是我的创建操作方法

  // GET: Students/Create
        public ActionResult RegisterStudent()
        
            ThorntreeDatabaseEntities db = new ThorntreeDatabaseEntities();
            ViewBag.ParentTypeID = new SelectList(db.ParentTypes, "Parent_TypeID", "Parent_Type");
            ViewBag.Student_TypeID = new SelectList(db.StudentTypes, "Student_TypeID", "Student_Type");
            ViewBag.ClassID = new SelectList(db.Classes, "ClassID", "Class_Name");
            ViewBag.GradeID = new SelectList(db.Grades, "GradeID", "Grade_Name");
            ViewBag.YearID = new SelectList(db.Years, "YearID", "Year1");
            return View();
        

        // POST: Students/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]

        public ActionResult RegisterStudent(StudentParentViewModel studentParentViewModel)
        

            ThorntreeDatabaseEntities db = new ThorntreeDatabaseEntities();
            List<Class> ClassList = db.Classes.ToList();
            ViewBag.ClassID = new SelectList(ClassList, "ClassID", "Class_Name");

            List<Grade> GradeList = db.Grades.ToList();
            ViewBag.GradeID = new SelectList(GradeList, "GradeID", "Grade_Name");

            List<Year> YearList = db.Years.ToList();
            ViewBag.YearID = new SelectList(YearList, "YearID", "Year1");

            List<StudentType> TypeList = db.StudentTypes.ToList();
            ViewBag.Student_TypeID = new SelectList(TypeList, "Student_TypeID", "Student_Type");

            List<ParentType> ParentList = db.ParentTypes.ToList();
            ViewBag.ParentTypeID = new SelectList(db.ParentTypes, "Parent_TypeID", "Parent_Type");
            //Parent 
            Parent parent = new Parent();
            parent.Title = studentParentViewModel.Title;
            parent.Parent_Name = studentParentViewModel.Parent_Name;
            parent.Parent_Middle_Name = studentParentViewModel.Parent_Middle_Name;
            parent.Parent_Surame = studentParentViewModel.Parent_Surame;
            parent.Home_Address = studentParentViewModel.Home_Address;
            parent.PID_Number = studentParentViewModel.PID_Number;
            parent.Contact_Number = studentParentViewModel.Contact_Number;
            parent.Alt_Contact_Number = studentParentViewModel.Alt_Contact_Number;
            parent.Email_Address = studentParentViewModel.Email_Address;
            parent.ParentTypeID = studentParentViewModel.ParentTypeID;
            db.Parents.Add(parent);
            db.SaveChanges();

            int latestParentID = parent.ParentID;

            Student student = new Student();
            student.Student_Name = studentParentViewModel.Student_Name;
            student.Student_Middle_Name = studentParentViewModel.Student_Middle_Name;
            student.Student_Lastname = studentParentViewModel.Student_Lastname;
            student.DoB = studentParentViewModel.DoB;
            student.SID_Number = studentParentViewModel.SID_Number;
            student.Gender = studentParentViewModel.Gender;
            student.Student_TypeID = studentParentViewModel.Student_TypeID;
            student.ParentID = latestParentID;
            db.Students.Add(student);
            db.SaveChanges();

            int latestStudentId = student.StudentID;

            CurrentYearStudent current = new CurrentYearStudent();
            current.ClassID = studentParentViewModel.ClassID;
            current.GradeID = studentParentViewModel.GradeID;
            current.YearID = studentParentViewModel.YearID;
            current.StudentID = latestStudentId;
            db.CurrentYearStudents.Add(current);
            db.SaveChanges();
            
            if (ModelState.IsValid)
            
                db.SaveChanges();
                return RedirectToAction("ViewStudents");
            
            return View(studentParentViewModel);

        

这是我有编辑按钮的地方,它在我的 ViewStudents.cshtml 上。它使用的是 CurrentYearStudents 模型,所以我无法访问 ParentID:

@model IEnumerable<Thorntree_Primary_School_INF_370_System.Models.CurrentYearStudent>
@
    ViewBag.Title = "View Students";



<html>
<head>
    <style>
        input[type="text"] 
            position: center;
            display: block;
            margin: 0 auto;
        

        input[type="file"] 
            position: center;
            display: block;
            margin: 0 auto;
        

        button[type="submit"] 
            position: center;
            display: block;
            margin: 0 auto;
        

        #tableHeadder 
            display: table-header-group;
        
    </style>
</head>
<body>
    <h2>Hello</h2>
    <h3>World</h3>

    <div class="container" style="outline:solid">
        <button class="btn bg-primary" style="">Back</button>

        <div @*style="text-align:center"*@>
            <h1>View Students</h1>
            @using (Html.BeginForm("ViewStudents", "Student", FormMethod.Get))
            
                //the following are search options
                <b> Search Option: </b>@Html.RadioButton("option", "FirstName") <label>Name</label> @Html.RadioButton("option", "Grade") <label>Grade</label>
                @Html.RadioButton("option", "Class")<label>Class</label> @Html.RadioButton("option", "None") <label>None</label> @Html.TextBox("search") <input type="submit" name="submit" value="Search" />
            
            <table class="table table-dark">
                <thead id="tableHeadder">
                    <tr>
                        <th scope="col">First Name</th>
                        <th scope="col">Last Name</th>
                        <th scope="col">Grade</th>
                        <th scope="col">Class</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.Student.Student_Name)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Student.Student_Lastname)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Grade.Grade_Name)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Class.Class_Name)
                            </td>
                            <td>
                                @Html.ActionLink("Edit", "UpdateStudent", new  id = item.StudentID ) |
                                @Html.ActionLink("Delete", "Delete", new  id = item.CurrentYearStudentID )
                            </td>
                        </tr>
                    
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>

这就是我的 3 个表在 SQL Server 中的样子

我真的可以使用一些帮助,我已经解决这个问题 2 天了,但无济于事。

如果您想查看我的更新操作,您可能会问,但是它几乎与添加操作相同,只是注释掉了,我不知道如何继续前进...请帮助,我'我现在很茫然,我不经常寻求帮助。 (我就是这样的人:()

编辑视图:

@model Thorntree_Primary_School_INF_370_System.Models.StudentParentViewModel
@
    ViewBag.Title = "Update Student";



@*<html>
<head>
@using (Html.BeginForm())

    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4 style="color:white">Parent of Student</h4>
    <h4 style="color:white">Parent of Student</h4>
    <h4 style="color:white">Parent of Student</h4>
    <h4 style="color:white">Parent of Student</h4>


    <h4>Parent of Student</h4>
    <hr />
    @Html.ValidationSummary(true, "", new  @class = "text-danger" )
    <div class="form-group">
        <label for="Title" class="control-label col-md-2">Title</label>
        <div class="col-md-10">
            @Html.EditorFor(model => model.Title, new  htmlAttributes = new  @class = "form-control", @value = Model.Title  )
            @Html.ValidationMessageFor(model => model.Title, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-group">
        <label for="Contact_Number" class="control-label col-md-2">Contact Number</label>
        <div class="col-md-10">
            @Html.EditorFor(model => model.Contact_Number, new  htmlAttributes = new  @class = "form-control", @value = Model.Contact_Number  )
            @Html.ValidationMessageFor(model => model.Contact_Number, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-group">
        <label for="Alt_Contact_Number" class="control-label col-md-2">Alternative Contact Number</label>
        <div class="col-md-10">
            @Html.EditorFor(model => model.Alt_Contact_Number, new  htmlAttributes = new  @class = "form-control", @value = Model.Alt_Contact_Number  )
            @Html.ValidationMessageFor(model => model.Alt_Contact_Number, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-group">
        <label for="Email_Address" class="control-label col-md-2">Email Address</label>
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email_Address, new  htmlAttributes = new  @class = "form-control", @value = Model.Email_Address  )
            @Html.ValidationMessageFor(model => model.Email_Address, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-group">
        <label for="Home_Address" class="control-label col-md-2">Home Address</label>
        <div class="col-md-10">
            @Html.EditorFor(model => model.Home_Address, new  htmlAttributes = new  @class = "form-control", @value = Model.Home_Address  )
            @Html.ValidationMessageFor(model => model.Home_Address, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-group">
        <label for="ParentTypeID" class="control-label col-md-2">Parent Type</label>
        <div class="col-md-10">
            @Html.DropDownList("ParentTypeID", null, htmlAttributes: new  @class = "form-control", @value = Model.ParentTypeID )
            @Html.ValidationMessageFor(model => model.ParentTypeID, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-group">
        <label for="Parent_Name" class="control-label col-md-2">Parent Name</label>
        <div class="col-md-10">
            @Html.EditorFor(model => model.Parent_Name, new  htmlAttributes = new  @class = "form-control", @value = Model.Parent_Name  )
            @Html.ValidationMessageFor(model => model.Parent_Name, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-group">
        <label for="Parent_Middle_Name" class="control-label col-md-2">Parent Middle Name</label>
        <div class="col-md-10">
            @Html.EditorFor(model => model.Parent_Middle_Name, new  htmlAttributes = new  @class = "form-control", @value = Model.Parent_Middle_Name  )
            @Html.ValidationMessageFor(model => model.Parent_Middle_Name, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-group">
        <label for="Parent_Surame" class="control-label col-md-2">Parent Surname</label>
        <div class="col-md-10">
            @Html.EditorFor(model => model.Parent_Surame, new  htmlAttributes = new  @class = "form-control", @value = Model.Parent_Surame  )
            @Html.ValidationMessageFor(model => model.Parent_Surame, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-group">
        <label for="PID_Number" class="control-label col-md-2">Parent ID Number</label>
        <div class="col-md-10">
            @Html.EditorFor(model => model.PID_Number, new  htmlAttributes = new  @class = "form-control", @value = Model.PID_Number  )
            @Html.ValidationMessageFor(model => model.PID_Number, "", new  @class = "text-danger" )
        </div>
    </div>

    <div class="form-horizontal">
        <h4>Student</h4>
        <hr />
        @Html.ValidationSummary(true, "", new  @class = "text-danger" )
        <div class="form-group">
            <label for="DoB" class="control-label col-md-2">Date of Birth</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.DoB, new  htmlAttributes = new  @class = "form-control", @value = Model.DoB  )
                @Html.ValidationMessageFor(model => model.DoB, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="DoB" class="control-label col-md-2">Gender</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Gender, new  htmlAttributes = new  @class = "form-control", @value = Model.Gender  )
                @Html.ValidationMessageFor(model => model.Gender, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Student_TypeID" class="control-label col-md-2">Student Type</label>
            <div class="col-md-10">
                @Html.DropDownList("Student_TypeID", null, htmlAttributes: new  @class = "form-control", @value = Model.Student_TypeID )
                @Html.ValidationMessageFor(model => model.Student_TypeID, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Student_Name" class="control-label col-md-2">Student Name</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Student_Name, new  htmlAttributes = new  @class = "form-control", @value = Model.Student_Name  )
                @Html.ValidationMessageFor(model => model.Student_Name, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Student_Middle_Name" class="control-label col-md-2">Student Middle Name</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Student_Middle_Name, new  htmlAttributes = new  @class = "form-control", @value = Model.Student_Middle_Name  )
                @Html.ValidationMessageFor(model => model.Student_Middle_Name, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Student_Lastname" class="control-label col-md-2">Student Surname</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Student_Lastname, new  htmlAttributes = new  @class = "form-control", @value = Model.Student_Lastname  )
                @Html.ValidationMessageFor(model => model.Student_Lastname, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="SID_Number" class="control-label col-md-2">Student ID Number</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.SID_Number, new  htmlAttributes = new  @class = "form-control", @value = Model.SID_Number  )
                @Html.ValidationMessageFor(model => model.SID_Number, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-horizontal">
            <h4>Register</h4>
            <hr />
            @Html.ValidationSummary(true, "", new  @class = "text-danger" )
        </div>

        <div class="form-group">
            <label for="GradeID" class="control-label col-md-2">Enrollment for Grade</label>
            <div class="col-md-10">
                @Html.DropDownList("GradeID", null, htmlAttributes: new  @class = "form-control", @value = Model.GradeID )
                @Html.ValidationMessageFor(model => model.GradeID, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="ClassID" class="control-label col-md-2">Enrollment for Class</label>
            <div class="col-md-10">
                @Html.DropDownList("ClassID", null, htmlAttributes: new  @class = "form-control", @value = Model.ClassID )
                @Html.ValidationMessageFor(model => model.ClassID, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="YearID" class="control-label col-md-2">Enrollment for Class</label>
            <div class="col-md-10">
                @Html.DropDownList("YearID", null, htmlAttributes: new  @class = "form-control", @value = Model.YearID )
                @Html.ValidationMessageFor(model => model.YearID, "", new  @class = "text-danger" )
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
</div>



<div>
    <button>@Html.ActionLink("Back to List", "ViewStudents")</button>

</div>

这是我当前的更新操作结果(是的,可能非常错误,但确实可以编辑 StudentID)

         public ActionResult UpdateStudent(int? id)
        
            if (id == null)
            
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                   

            ThorntreeDatabaseEntities db = new ThorntreeDatabaseEntities();

           

            List<Class> ClassList = db.Classes.ToList();
            ViewBag.ClassID = new SelectList(ClassList, "ClassID", "Class_Name");

            List<Grade> GradeList = db.Grades.ToList();
            ViewBag.GradeID = new SelectList(GradeList, "GradeID", "Grade_Name");

            List<Year> YearList = db.Years.ToList();
            ViewBag.YearID = new SelectList(YearList, "YearID", "Year1");

            List<StudentType> TypeList = db.StudentTypes.ToList();
            ViewBag.Student_TypeID = new SelectList(TypeList, "Student_TypeID", "Student_Type");

            List<ParentType> ParentList = db.ParentTypes.ToList();
            ViewBag.ParentTypeID = new SelectList(db.ParentTypes, "Parent_TypeID", "Parent_Type");

            StudentParentViewModel viewModel = new StudentParentViewModel();

            Student student = db.Students.Find(id);
            Parent parent = db.Parents.Find(student.ParentID);

            db.Students.Where(x => x.StudentID == parent.ParentID);
            viewModel.Title = parent.Title;
            viewModel.Parent_Name = parent.Parent_Name;
            viewModel.Parent_Middle_Name = parent.Parent_Middle_Name;
            viewModel.Parent_Surame = parent.Parent_Surame;
            viewModel.Home_Address = parent.Home_Address;
            viewModel.PID_Number = parent.PID_Number;
            viewModel.Contact_Number = parent.Contact_Number;
            viewModel.Alt_Contact_Number = parent.Alt_Contact_Number;
            viewModel.Email_Address = parent.Email_Address;
            viewModel.ParentTypeID = parent.ParentTypeID;
            viewModel.ParentID = parent.ParentID;

            int latestParentID = parent.ParentID;
            viewModel.StudentID = student.StudentID;
            viewModel.Student_Name = student.Student_Name;
            viewModel.Student_Middle_Name = student.Student_Middle_Name;
            viewModel.Student_Lastname = student.Student_Lastname;
            viewModel.DoB = student.DoB;
            viewModel.SID_Number = student.SID_Number;
            viewModel.Gender = student.Gender;
            viewModel.Student_TypeID = student.Student_TypeID;
            student.ParentID = latestParentID;

            int latestStudentId = student.StudentID;


            db.Students.Where(x => x.StudentID == parent.ParentID);
            viewModel.Title = parent.Parent_Name;
            viewModel.Parent_Name = parent.Parent_Name;
            viewModel.Parent_Middle_Name = parent.Parent_Middle_Name;
            viewModel.Parent_Surame = parent.Parent_Surame;
            viewModel.Home_Address = parent.Home_Address;
            viewModel.PID_Number = parent.PID_Number;
            viewModel.Contact_Number = parent.Contact_Number;
            viewModel.Alt_Contact_Number = parent.Alt_Contact_Number;
            viewModel.Email_Address = parent.Email_Address;
            viewModel.ParentTypeID = parent.ParentTypeID;


            CurrentYearStudent current = new CurrentYearStudent();
            viewModel.ClassID = current.ClassID;
            viewModel.GradeID = current.GradeID;
            viewModel.YearID = current.YearID;
            viewModel.StudentID = latestStudentId;




            ViewBag.ParentTypeID = new SelectList(db.ParentTypes, "Parent_TypeID", "Parent_Type");
            ViewBag.Student_TypeID = new SelectList(db.StudentTypes, "Student_TypeID", "Student_Type");
            ViewBag.ClassID = new SelectList(db.Classes, "ClassID", "Class_Name");
            ViewBag.GradeID = new SelectList(db.Grades, "GradeID", "Grade_Name");
            ViewBag.YearID = new SelectList(db.Years, "YearID", "Year1");
            return View(viewModel);
        

 // POST: Students/Update/5
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UpdateStudent(StudentParentViewModel viewModel)
        

            ThorntreeDatabaseEntities db = new ThorntreeDatabaseEntities();
            List<Class> ClassList = db.Classes.ToList();
            ViewBag.ClassID = new SelectList(ClassList, "ClassID", "Class_Name");

            List<Grade> GradeList = db.Grades.ToList();
            ViewBag.GradeID = new SelectList(GradeList, "GradeID", "Grade_Name");

            List<Year> YearList = db.Years.ToList();
            ViewBag.YearID = new SelectList(YearList, "YearID", "Year1");

            List<StudentType> TypeList = db.StudentTypes.ToList();
            ViewBag.Student_TypeID = new SelectList(TypeList, "Student_TypeID", "Student_Type");

            List<ParentType> ParentList = db.ParentTypes.ToList();
            ViewBag.ParentTypeID = new SelectList(db.ParentTypes, "Parent_TypeID", "Parent_Type");


                Parent parent = new Parent();
                parent.Title = viewModel.Title;
                parent.Parent_Name = viewModel.Parent_Name;
                parent.Parent_Middle_Name = viewModel.Parent_Middle_Name;
                parent.Parent_Surame = viewModel.Parent_Surame;
                parent.Home_Address = viewModel.Home_Address;
                parent.PID_Number = viewModel.PID_Number;
                parent.Contact_Number = viewModel.Contact_Number;
                parent.Alt_Contact_Number = viewModel.Alt_Contact_Number;
                parent.Email_Address = viewModel.Email_Address;
                parent.ParentTypeID = viewModel.ParentTypeID;


                int latestParentID = parent.ParentID;

                Student student = new Student();
                student.Student_Name = viewModel.Student_Name;
                student.Student_Middle_Name = viewModel.Student_Middle_Name;
                student.Student_Lastname = viewModel.Student_Lastname;
                student.DoB = viewModel.DoB;
                student.SID_Number = viewModel.SID_Number;
                student.Gender = viewModel.Gender;
                student.Student_TypeID = viewModel.Student_TypeID;
                student.ParentID = latestParentID;


                int latestStudentId = student.StudentID;

                ViewBag.ParentID = new SelectList(db.Parents, "ParentID", "Title", student.ParentID);

                CurrentYearStudent current = new CurrentYearStudent();
                current.ClassID = viewModel.ClassID;
                current.GradeID = viewModel.GradeID;
                current.YearID = viewModel.YearID;
                current.StudentID = latestStudentId;
                db.CurrentYearStudents.Add(current);


                ViewBag.Student_TypeID = new SelectList(db.StudentTypes, "Student_TypeID", "Student_Type", student.Student_TypeID);

                if (ModelState.IsValid)
                
                    db.Entry(student).State = EntityState.Modified;
                    db.Entry(parent).State = EntityState.Modified;
                    db.Entry(current).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ViewStudents");
                
                return View(viewModel);
        

【问题讨论】:

我想知道您的期望吗?你到底想从你的视图模型更新到 3 个表中的什么?你是说这个StudentParentViewModel viewModel?如果是的话,它的观点在哪里? 添加必要的信息,以便您的问题可以轻松重现。这个问题很难重现,因为它有许多未包含在内的依赖项。尝试添加示例以加载您的 RegisterStudent 视图和 edit 上下文,以便更轻松地向您展示您陷入困境的后续步骤。 @MdFaridUddinKiron 所以我正在尝试更新学生表中的所有详细信息,然后是学生所属家长的详细信息以及学生所在的年级、班级和年级,只有CurrentYearStudent 表显示在我的视图中。我添加了编辑页面的视图,他们在其中更改了详细信息。 如果它可以重现,那么指出你的问题可能会更容易 @MdFaridUddinKiron 那么有人需要什么来重现它呢?我的 CreateStudent 视图?我当前的非工作更新操作结果? 【参考方案1】:

让我们从下面的评论开始吧..

“但是我不知道为什么我的 Http Post: 不起作用”

因为你的这个代码@using (Html.BeginForm()) 而应该是@using (Html.BeginForm("ActionName", "controllerName"))

如何使用我的视图模型更新我的 3 个表:

请按照以下步骤操作

首先加载列表的学生模型:

public class Student

    [Key]
    public int StudentID  get; set; 
    public string DoB  get; set; 
    public string Gender  get; set; 
    public bool Remember  get; set; 
    public string Student_Name  get; set; 
    public string Student_Middle_Name  get; set; 
    public string Student_Lastname  get; set; 
    public long SID_Number  get; set; 
    [ForeignKey("Parent")]
    public int ParentID  get; set; 
    [ForeignKey("StudentType")]
    public int StudentTypeID  get; set; 
    [ForeignKey("Grades")]
    public int GradeID  get; set; 
    [ForeignKey("Year")]
    public int YearID  get; set; 
    [ForeignKey("ClassType")]
    public int ClassTypeID  get; set; 
    public virtual StudentType StudentType  get; set; 
    public virtual Parent Parent  get; set; 
    public virtual Grades Grades  get; set; 
    public virtual Year Year  get; set; 
    public virtual ClassType ClassType  get; set; 

加载学生列表的控制器:

我不确定您使用哪种方法绑定数据database firstcode first。我正在绑定数据库中的数据,如下所示:

   public IActionResult GetAllStudents()
        
            var obj = _context.Students.Include(st => st.StudentType).Include(pt => pt.Parent).Include(y => y.Year).Include(ct => ct.ClassType).ToList();
            return View(obj);
        

查看学生名单:

@model IEnumerable<MVCApps.Models.Student>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table table table-bordered">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.StudentID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DoB)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Gender)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Remember)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Student_Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Student_Middle_Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Student_Lastname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.SID_Number)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Parent.Parent_Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.StudentType)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.GradeID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.YearID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ClassTypeID)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) 
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.StudentID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DoB)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Gender)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Remember)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Student_Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Student_Middle_Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Student_Lastname)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SID_Number)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Parent.Parent_Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StudentType.StudentTypeName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Grades.GradeName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Year.YearName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ClassType.ClassTypeName)
            </td>
            <td>
                @Html.ActionLink("Edit", "UpdateStudentById", new  id = item.StudentID , new  @class = "btn btn-primary" )
            </td>
            <td>
                @Html.ActionLink("Details", "Details", new  /* id=item.PrimaryKey */ , new  @class = "btn btn-info" )
            </td>
            <td>
                @Html.ActionLink("Delete", "Delete", new  /* id=item.PrimaryKey */ , new  @class = "btn btn-danger" )
            </td>
        </tr>

    </tbody>
</table>

注意:为了加载这个视图,我正在使用你的 student 域类。

输出:

更新操作:

当您单击编辑按钮时,它应该使用viewmodel 类型加载您的视图。 所以我们将在哪里使用你的StudentParentViewModel

查看模型:

public class StudentParentViewModel
    
        //parent
        public int ParentID  get; set; 
        public string Title  get; set; 
        public int Contact_Number  get; set; 
        public Nullable<int> Alt_Contact_Number  get; set; 
        public string Email_Address  get; set; 
        public string Home_Address  get; set; 
        public int ParentTypeID  get; set; 
        public string Parent_Name  get; set; 
        public string Parent_Middle_Name  get; set; 
        public string Parent_Surame  get; set; 
        public long PID_Number  get; set; 

        //student
        public int StudentID  get; set; 
        public DateTime DoB  get; set; 
        public string Gender  get; set; 
        public bool Remember  get; set; 
        public int StudentTypeID  get; set; 
        public string Student_Name  get; set; 
        public string Student_Middle_Name  get; set; 
        public string Student_Lastname  get; set; 
        public long SID_Number  get; set; 

        //current year student
        public int CurrentYearStudentID  get; set; 
        public Nullable<int> GradeID  get; set; 
        public Nullable<int> ClassTypeID  get; set; 
        public Nullable<int> YearID  get; set; 
    

更新控制器:

当您单击edit 按钮时,它将使用特定的id 击中控制器。所以这里我们通过id获取详细信息后绑定ViewModel,返回加载视图update view

  public ActionResult UpdateStudentById(int? id)
        
            //Get student by ID
            var studentObject = _context.Students.Include(st => st.StudentType).Include(pt => pt.Parent).Where(sid => sid.StudentID == id).FirstOrDefault();
            //Bind Dropdown
            List<StudentType> studentType = _context.studentTypes.ToList();
            ViewBag.StudentTypeID = new SelectList(studentType, "StudentTypeID", "StudentTypeName");

            //Bind ParentType Dropdown
            List<ParentType> parentTypeList = _context.ParentTypes.ToList();
            ViewBag.ParentTypeID = new SelectList(parentTypeList, "ParentTypeID", "ParentTypeName");

            //class type
            List<ClassType> classType = _context.classTypes.ToList();
            ViewBag.ClassTypeID = new SelectList(classType, "ClassTypeID", "ClassTypeName");
            //grade
            List<Grades> grades = _context.Grades.ToList();
            ViewBag.GradeID = new SelectList(grades, "GradeID", "GradeName");

            //class type
            List<Year> years = _context.Years.ToList();
            ViewBag.YearID = new SelectList(years, "YearID", "YearName");

            //Bind to View Model
            StudentParentViewModel viewModel = new StudentParentViewModel();

            //Student student = _context.Students.Find(id);
            //  Parent parent = _context.parents.Find(parent.ParentID);

            //db.Students.Where(x => x.StudentID == parent.ParentID);
            viewModel.Title = studentObject.Parent.Title;
            viewModel.Parent_Name = studentObject.Parent.Parent_Name;
            viewModel.Parent_Middle_Name = studentObject.Parent.Parent_Middle_Name;
            viewModel.Parent_Surame = studentObject.Parent.Parent_Surame;
            viewModel.Home_Address = studentObject.Parent.Home_Address;
            viewModel.PID_Number = studentObject.Parent.PID_Number;
            viewModel.Contact_Number = studentObject.Parent.Contact_Number;
            viewModel.Alt_Contact_Number = studentObject.Parent.Alt_Contact_Number;
            viewModel.Email_Address = studentObject.Parent.Email_Address;
            viewModel.ParentTypeID = studentObject.Parent.ParentTypeID;
            viewModel.ParentID = studentObject.Parent.ParentID;

            int latestParentID = studentObject.Parent.ParentID;
            viewModel.StudentID = studentObject.StudentID;
            viewModel.Student_Name = studentObject.Student_Name;
            viewModel.Student_Middle_Name = studentObject.Student_Middle_Name;
            viewModel.Student_Lastname = studentObject.Student_Lastname;
            viewModel.DoB = Convert.ToDateTime(studentObject.DoB);
            viewModel.SID_Number = studentObject.SID_Number;
            viewModel.Gender = studentObject.Gender;
            viewModel.StudentTypeID = studentObject.StudentTypeID;
            studentObject.ParentID = latestParentID;

            int latestStudentId = studentObject.StudentID;



            return View(viewModel);
           
        

更新视图:

@model MVCApps.Models.StudentParentViewModel

@using (Html.BeginForm("UpdateStudent", "Student"))

    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4 style="color:white">Parent of Student</h4>
    <h4 style="color:white">Parent of Student</h4>
    <h4 style="color:white">Parent of Student</h4>
    <h4 style="color:white">Parent of Student</h4>
    <div class="form-horizontal">
        <h4>Student</h4>
        <hr />
        @Html.ValidationSummary(true, "", new  @class = "text-danger" )
        <div class="form-group" hidden>
            <label for="DoB" class="control-label col-md-2">StudentID</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.StudentID, new  htmlAttributes = new  @class = "form-control", @value = Model.StudentID  )
                @Html.ValidationMessageFor(model => model.StudentID, "", new  @class = "text-danger" )
            </div>
        </div>
        @Html.ValidationSummary(true, "", new  @class = "text-danger" )
        <div class="form-group">
            <label for="DoB" class="control-label col-md-2">Date of Birth</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.DoB, new  htmlAttributes = new  @class = "form-control", @value = Model.DoB  )
                @Html.ValidationMessageFor(model => model.DoB, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="DoB" class="control-label col-md-2">Gender</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Gender, new  htmlAttributes = new  @class = "form-control", @value = Model.Gender  )
                @Html.ValidationMessageFor(model => model.Gender, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Student_TypeID" class="control-label col-md-2">Student Type</label>
            <div class="col-md-10">
                @Html.DropDownList("StudentTypeID", null, htmlAttributes: new  @class = "form-control", @value = Model.StudentTypeID )
                @Html.ValidationMessageFor(model => model.StudentTypeID, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Student_Name" class="control-label col-md-2">Student Name</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Student_Name, new  htmlAttributes = new  @class = "form-control", @value = Model.Student_Name  )
                @Html.ValidationMessageFor(model => model.Student_Name, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Student_Middle_Name" class="control-label col-md-2">Student Middle Name</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Student_Middle_Name, new  htmlAttributes = new  @class = "form-control", @value = Model.Student_Middle_Name  )
                @Html.ValidationMessageFor(model => model.Student_Middle_Name, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Student_Lastname" class="control-label col-md-2">Student Surname</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Student_Lastname, new  htmlAttributes = new  @class = "form-control", @value = Model.Student_Lastname  )
                @Html.ValidationMessageFor(model => model.Student_Lastname, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="SID_Number" class="control-label col-md-2">Student ID Number</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.SID_Number, new  htmlAttributes = new  @class = "form-control", @value = Model.SID_Number  )
                @Html.ValidationMessageFor(model => model.SID_Number, "", new  @class = "text-danger" )
            </div>
        </div>

        <h4>Parent of Student</h4>
        <hr />
        @Html.ValidationSummary(true, "", new  @class = "text-danger" )
        <div class="form-group" hidden>
            <label for="Title" class="control-label col-md-2">StudentID</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.ParentID, new  htmlAttributes = new  @class = "form-control", @value = Model.ParentID  )
                @Html.ValidationMessageFor(model => model.ParentID, "", new  @class = "text-danger" )
            </div>
        </div>
        <div class="form-group">
            <label for="Title" class="control-label col-md-2">Title</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new  htmlAttributes = new  @class = "form-control", @value = Model.Title  )
                @Html.ValidationMessageFor(model => model.Title, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Contact_Number" class="control-label col-md-2">Contact Number</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Contact_Number, new  htmlAttributes = new  @class = "form-control", @value = Model.Contact_Number  )
                @Html.ValidationMessageFor(model => model.Contact_Number, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Alt_Contact_Number" class="control-label col-md-2">Alternative Contact Number</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Alt_Contact_Number, new  htmlAttributes = new  @class = "form-control", @value = Model.Alt_Contact_Number  )
                @Html.ValidationMessageFor(model => model.Alt_Contact_Number, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Email_Address" class="control-label col-md-2">Email Address</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email_Address, new  htmlAttributes = new  @class = "form-control", @value = Model.Email_Address  )
                @Html.ValidationMessageFor(model => model.Email_Address, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Home_Address" class="control-label col-md-2">Home Address</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Home_Address, new  htmlAttributes = new  @class = "form-control", @value = Model.Home_Address  )
                @Html.ValidationMessageFor(model => model.Home_Address, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="ParentTypeID" class="control-label col-md-2">Parent Type</label>
            <div class="col-md-10">
                @Html.DropDownList("ParentTypeID", null, htmlAttributes: new  @class = "form-control", @value = Model.ParentTypeID )
                @Html.ValidationMessageFor(model => model.ParentTypeID, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Parent_Name" class="control-label col-md-2">Parent Name</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Parent_Name, new  htmlAttributes = new  @class = "form-control", @value = Model.Parent_Name  )
                @Html.ValidationMessageFor(model => model.Parent_Name, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Parent_Middle_Name" class="control-label col-md-2">Parent Middle Name</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Parent_Middle_Name, new  htmlAttributes = new  @class = "form-control", @value = Model.Parent_Middle_Name  )
                @Html.ValidationMessageFor(model => model.Parent_Middle_Name, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="Parent_Surame" class="control-label col-md-2">Parent Surname</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Parent_Surame, new  htmlAttributes = new  @class = "form-control", @value = Model.Parent_Surame  )
                @Html.ValidationMessageFor(model => model.Parent_Surame, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="PID_Number" class="control-label col-md-2">Parent ID Number</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.PID_Number, new  htmlAttributes = new  @class = "form-control", @value = Model.PID_Number  )
                @Html.ValidationMessageFor(model => model.PID_Number, "", new  @class = "text-danger" )
            </div>
        </div>



        <div class="form-horizontal">
            <h4>Register</h4>
            <hr />
            @Html.ValidationSummary(true, "", new  @class = "text-danger" )
        </div>

        <div class="form-group">
            <label for="GradeID" class="control-label col-md-2">Enrollment for Grade</label>
            <div class="col-md-10">
                @Html.DropDownList("GradeID", null, htmlAttributes: new  @class = "form-control", @value = Model.GradeID )
                @Html.ValidationMessageFor(model => model.GradeID, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="ClassID" class="control-label col-md-2">Enrollment for Class</label>
            <div class="col-md-10">
                @Html.DropDownList("ClassTypeID", null, htmlAttributes: new  @class = "form-control", @value = Model.ClassTypeID )
                @Html.ValidationMessageFor(model => model.ClassTypeID, "", new  @class = "text-danger" )
            </div>
        </div>

        <div class="form-group">
            <label for="YearID" class="control-label col-md-2">Enrollment for Class</label>
            <div class="col-md-10">
                @Html.DropDownList("YearID", null, htmlAttributes: new  @class = "form-control", @value = Model.YearID )
                @Html.ValidationMessageFor(model => model.YearID, "", new  @class = "text-danger" )
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Update" class="btn btn-success" />
            </div>
            
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                @Html.ActionLink("List", "GetAllStudents", new  /* id=item.PrimaryKey */ , new  @class = "btn btn-info" )
            </div>
        </div>
    </div>
</div>


更新视图的输出:

更新控制器:

当更新视图被加载时,在必要的更新后,我们将点击更新按钮,它将更新的数据发送到UpdateStudent控制器,我们的StudentParentViewModel如下所示:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UpdateStudent(StudentParentViewModel viewModel)
    
        //If either of this is empty then add
        if (viewModel.StudentID == 0 && viewModel.ParentID == 0)
        
            //Add Or Create 
            //Bind Student
            var student = new Student();
            student.Student_Name = viewModel.Student_Name;
            student.Student_Middle_Name = viewModel.Student_Middle_Name;
            student.Student_Lastname = viewModel.Student_Lastname;
            student.Gender = viewModel.Gender;
            student.StudentTypeID = viewModel.StudentTypeID;
            student.ParentID = viewModel.ParentID;
            student.DoB = viewModel.DoB.ToString();
            student.GradeID = viewModel.GradeID??0;
            student.ClassTypeID = viewModel.ClassTypeID??0;
            student.YearID = viewModel.YearID??0;
            student.ClassTypeID = viewModel.ClassTypeID??0;


            _context.Students.Add(student);
            _context.SaveChanges();

            //Bind Parent

            Parent parent = new Parent();
            parent.Title = viewModel.Title;
            parent.Parent_Name = viewModel.Parent_Name;
            parent.Parent_Middle_Name = viewModel.Parent_Middle_Name;
            parent.Parent_Surame = viewModel.Parent_Surame;
            parent.Home_Address = viewModel.Home_Address;
            parent.PID_Number = viewModel.PID_Number;
            parent.Contact_Number = viewModel.Contact_Number;
            parent.Alt_Contact_Number = viewModel.Alt_Contact_Number;
            parent.Email_Address = viewModel.Email_Address;
            parent.ParentTypeID = viewModel.ParentTypeID;
            _context.parents.Add(parent);
            _context.SaveChanges();
        
        else
        
            var findStudent = _context.Students.Find(viewModel.StudentID);
            //Bind to student table
            Student objStudent = new Student();
            findStudent.Student_Name = viewModel.Student_Name;
            findStudent.Student_Middle_Name = viewModel.Student_Middle_Name;
            findStudent.Student_Lastname = viewModel.Student_Lastname;
            findStudent.DoB = viewModel.DoB.ToString();
            findStudent.SID_Number = viewModel.SID_Number;
            findStudent.Gender = viewModel.Gender;
            findStudent.StudentTypeID = viewModel.StudentTypeID;
            findStudent.ParentID = viewModel.ParentID;
            findStudent.ClassTypeID = viewModel.ClassTypeID??0;
            findStudent.GradeID =(int) viewModel.GradeID;
            findStudent.ParentID = viewModel.ParentID;

            //Bind the parent table
            var findParent = _context.parents.Find(viewModel.ParentID);
            Parent objParent = new Parent();
            findParent.Title = viewModel.Title;
            findParent.Parent_Name = viewModel.Parent_Name;
            findParent.Parent_Middle_Name = viewModel.Parent_Middle_Name;
            findParent.Parent_Surame = viewModel.Parent_Surame;
            findParent.Home_Address = viewModel.Home_Address;
            findParent.PID_Number = viewModel.PID_Number;
            findParent.Contact_Number = viewModel.Contact_Number;
            findParent.Alt_Contact_Number = viewModel.Alt_Contact_Number;
            findParent.Email_Address = viewModel.Email_Address;
            findParent.ParentTypeID = viewModel.ParentTypeID;
            _context.SaveChanges();
        
        //Bind Dropdown
        List<StudentType> studentType = _context.studentTypes.ToList();
        ViewBag.StudentTypeID = new SelectList(studentType, "StudentTypeID", "StudentTypeName");

        //Bind ParentType Dropdown
        List<ParentType> parentTypeList = _context.ParentTypes.ToList();
        ViewBag.ParentTypeID = new SelectList(parentTypeList, "ParentTypeID", "ParentTypeName");

        //class type
        List<ClassType> classType = _context.classTypes.ToList();
        ViewBag.ClassTypeID = new SelectList(classType, "ClassTypeID", "ClassTypeName");
        //grade
        List<Grades> grades = _context.Grades.ToList();
        ViewBag.GradeID = new SelectList(grades, "GradeID", "GradeName");

        //class type
        List<Year> years = _context.Years.ToList();
        ViewBag.YearID = new SelectList(years, "YearID", "YearName");
       // return Redirect()
        return RedirectToAction("GetAllStudents");
    

注意:StudentParent 表一样,您可以将视图模型数据绑定到Nth 表编号,就像我上面展示的方式一样。最后将更新页面重定向到列表页面。

最终输出:

注意: 我发现您的代码中有一些不一致之处,您可以改进,例如如果您将类属性写为 Parent_Name 那么 在任何地方都遵循under score 约定。如果camel case parentName 然后camel case 无处不在,就像明智的pascal case ParentTypeID 然后遵循减少代码异味和 使代码更具可读性。

希望以上步骤能帮助您解决问题。

【讨论】:

我会永远记住你的名字作为我的救世主,我会注意命名约定。打心眼里谢谢谢谢谢谢! 另外一点,我想你在哪里说 _context.Students.Add(student);_context.parents.Add(parent); 所以对于这两行,我认为它导致数据添加一个条目,其中包含与更新相对应的新更新它。您可能知道问题所在吗? @Md Farid Uddin Kiron 一开始你可以注意到在更新方法中,我检查了学生和父母的id,这意味着当我们创建新的学生和父母时,当时没有id,所以当有id时表示我们正在更新,当没有 id 时表示需要添加。只需自己检查逻辑,然后让我知道您的反馈。 我尝试使用更新方法来添加和更新请求。你可以把它分开,这样就不会有挑战了。 您可以省略if 部分并删除else 并将代码保留在仅用于更新的else 部分中。希望你明白我的意思。

以上是关于如何在 ASP.NET Core MVC 中使用我的视图模型更新我的 3 个表(我已经有了 Create)的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ASP.NET Core MVC 中使用 ADO.NET 向存储过程添加参数?

如何在 ASP.NET Core 5 MVC (.NET 5) 中获取记录的用户数据?

如何使用 MVC 的内容协商在 ASP.NET Core MVC 中间件中返回响应?

使用 ASP.NET Core MVC,如何显示图像?

如何使用 C# 在 ASP.NET Core 3.1 MVC 中使用会话变量

如何使用 asp.net core mvc 在引导模式中加载 AngularJS 代码