AJAX调用删除DB记录

时间:2014-09-29 02:34:36

标签: ajax asp.net-mvc model-view-controller view controller

我有一个显示个人课程列表的页面。该页面允许用户从所选用户课程列表中删除课程。单击删除按钮时,应从课程列表中删除该课程,并重新显示课程列表。在我的控制器中,我试图删除记录,然后调用listCourses视图(最初列出用户的课程的视图)但是当我单击删除按钮时,没有任何反应。以下是我的代码:

选择学生的视图:

@foreach (var stu in Model)
        {
            <tr>
                <td class="stulisttd">
                    @stu.Id
                </td>
                <td class="stulisttd">
                    @stu.fname
                </td>
                <td class="stulisttd">
                    @stu.lname
                </td>
                <td class="thEditButton">
                    @using (Ajax.BeginForm("addCourses", "ManageStudentCourses", stuEditCourses))
                    {
                        @Html.TextBox("stuId", (string)stu.Id, new { @class = "listCourseText" })
                        <input type="submit" id="addCourses" name="addCourses" value='Add Courses' class="AllTracksButtons" />
                    }
                </td>
                <td class="thEditButton">
                    @using (Ajax.BeginForm("listCourses", "ManageStudentCourses", stuEditCourses))
                    {
                        @Html.TextBox("stuId", (string)stu.Id, new { @class = "listCourseText" })
                        <input type="submit" value="List Courses" class="AllTracksButtons" id=@stu.Id />
                    }
                </td>
            </tr>
        }

查看列出所选用户的课程:

@foreach (var course in Model)
    {
        using (Ajax.BeginForm("deleteCourses", "ManageStudentCourses", stuListEditCourses))
        {
            <tr class="trCourseList">

                <td>
                    @course.courseAbNum
                    @Html.TextBox("courseAbNum", (string)course.courseAbNum, new { @class = "editCourseText" })
                </td>
                <td>
                    @course.courseDesc
                </td>
                <td>
                    @course.status
                </td>
                <td>
                    @course.grade
                </td>
                <td>
                    @course.credits
                </td>
                <td>
                    <input type="submit" value="Delete" class="courseListButtons" id="displayEdit" />

                </td>
            </tr>
        }

deleteCourses控制器:

public PartialViewResult deleteCourses(string courseAbNum)
    {
        KuPlanEntities db = new KuPlanEntities();

        var deleteCourse = (from course in db.COURSE_TAKEN
                            where course.courseAbNum == courseAbNum && course.Id == "000160228"
                            select course);
        foreach (var course in deleteCourse)
        {
            db.COURSE_TAKEN.Remove(course);
            db.SaveChanges();
        }
        var stuCourses = (from studCourse in db.COURSE_TAKEN
                          join course in db.COURSEs on studCourse.courseAbNum equals course.courseAbNum
                          where studCourse.Id == "000160228"
                          select new courseListViewModel { id = studCourse.Id, courseAbNum = studCourse.courseAbNum, status = studCourse.status, grade = studCourse.grade, courseDesc = course.courseDesc, credits = course.credits });

        return PartialView("~/Views/ManageStudentCourses/listCourses.cshtml", stuCourses);
    }        

1 个答案:

答案 0 :(得分:0)

我认为您将遇到此代码的问题

foreach (var course in deleteCourse)
    {
        db.COURSE_TAKEN.Remove(course);
        db.SaveChanges();
    }

您可能会收到错误“收集已修改;枚举操作可能无法执行。”

要解决此问题,您可以使用此黑客

 while (db.COURSE_TAKEN.Where(x => x.ID == ID).Count() != 0)
        {
            COURSE_TAKEN course = db.COURSE_TAKEN.Where(x => x.ID == ID).FirstOrDefault();
            db.COURSE_TAKEN.Remove(course);
            db.SaveChanges();
        }

希望这有帮助。