ahmed salah

ahmed salah

  • NA
  • 530
  • 148k

save courses removed by jquery when click submit button

Sep 8 2016 5:15 PM

In edit view When Remove courses by JQUERY and click submit button . changes not saved in database

Detailes

meaning removed course still exist in database after click submit

although it removed by jquery

I try more time to solve this point

i need to check code after else in Edit action

image describe what i need

 

no save to course that removed

code

  1. // remove courses success by jquery  
  2.      <script>  
  3.             $(function () {  
  4.                 $(document).on("click"".remove", function (e) {  
  5.                     e.preventDefault();  
  6.                     $(this).closest(".course-item").remove();  
  7.                 });  
  8.                 $('#AvailableCourses').change(function () {  
  9.   
  10.                     var existingCourses = $("input[name='CourseIds']")  
  11.                         .map(function () { return this.value; }).get();  
  12.                     }  
  13.                 });  
  14.             });  
  15.             </script>  
  16.   
  17.     // here i write code for save courses added or courses removed  
  18.     [HttpPost]  
  19.             public ActionResult Edit(EditEmployeeVm model)  
  20.   
  21.             {  
  22.                 var origunalCourseIds = TempData["OriginalId"as int[];  
  23.                 var newCourceIds = model.CourseIds;  
  24.                 foreach (var item in origunalCourseIds)  
  25.                 {  
  26.                     if (newCourceIds.Any(m => m == item))  
  27.                     {  
  28.     // when add new course then click submit save success in database  
  29.                         var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);  
  30.                         foreach (var couseid in newCourceIds)  
  31.                         {  
  32.                             db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });  
  33.                             db.SaveChanges();  
  34.   
  35.                         }  
  36.   
  37.                     }  
  38.                     **else  
  39.                     {  
  40.     **//remove courses then click submit not save removed item**  
  41.                         var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);  
  42.                         foreach (var couseid in newCourceIds)  
  43.                         {  
  44.                             db.EmployeeCourses.Remove(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });  
  45.                             db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });  
  46.                             db.SaveChanges();  
  47.   
  48.                         }  
  49.   
  50.   
  51.                     }  
  52.                 }**  
  53.                 return View();  
  54.     }  
  55.     //custom model using to my code  
  56.     //save in employeecourse table  
  57.     public class EditEmployeeVm  
  58.         {  
  59.             public int Id { setget; }  
  60.             public List<SelectListItem> Courses { getset; }  
  61.             public int[] CourseIds { setget; }  
  62.             public List<CourseVm> ExistingCourses { setget; }  
  63.         }  
  64.         public class CourseVm  
  65.         {  
  66.             public int Id { setget; }  
  67.             public string Name { setget; }  
  68.         }  
  69.     }  
  70.     //in Edit [HTTPGET]  Retrieve courses detailes for employee i need to edit  
  71.      public ActionResult Edit(int id)  
  72.             {  
  73.   
  74.                 var vm = new EditEmployeeVm { Id = id };  
  75.   
  76.   
  77.                 vm.CourseIds = vm.ExistingCourses.Select(g => g.Id).ToArray();  
  78.                 TempData["OriginalId"] = vm.CourseIds;  
  79.                 return View(vm);  
  80.             }  

Answers (5)