2014-09-29 133 views
0

我有一個頁面顯示個人課程列表。該頁面允許用戶從所選用戶課程列表中刪除課程。當單擊刪除按鈕時,應該從課程列表中刪除該課程,並重新顯示課程列表。在我的控制器中,我試圖刪除記錄,然後調用listCourses視圖(查看最初列出用戶的課程),但是當我單擊刪除按鈕時,什麼都不會發生。下面是我的代碼:AJAX呼叫刪除數據庫記錄

查看選擇學生:

@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" [email protected] /> 
        } 
       </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); 
    }   
+0

什麼是'stuListEditCourses'在'Ajax.BeginForm()'?你是控制器被命中? – 2014-09-29 02:49:45

+0

它告訴ajax在頁面上替換什麼 – Stc5097 2014-09-29 03:05:18

+0

所以它的'AjaxOptions'?就像'new AjaxOptions {UpdateTargetId =「someElement」}''是你的控制器被擊中嗎? – 2014-09-29 03:09:30

回答

0

我想你將會對此代碼有問題

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

您可能會收到錯誤「Collection was modified;枚舉操作可能無法執行。」

要解決這個問題,你可以使用這個破解

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(); 
     } 

希望這有助於。