ahmed salah

ahmed salah

  • NA
  • 530
  • 148.8k

cannot save changes of courses if remove in edit httppost

Sep 10 2016 8:56 AM
Problem
when remove course then click submit not save changes although
it removed from client side by jquery
Details 
in edit view for every employee i need to do changes by remove or add new courses for employee
if i add new courses then click submit button it save what i do
but if i remove course from courses then click submit it will not save courses i removed
so that i need to check what is wrong in my code
my code working without any problem  but only have problem
i cannot save courses removed in database in employeecourse table when click submit 
employeecourse table have Id,EmployeeId,CourseId
jquery remove client side attached with my question 
code 
my code as following 
  1.  [HttpPost]  
  2.         public ActionResult Edit(EditEmployeeVm model)  
  3.         {  
  4.             var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);  
  5.             foreach (var couseid in model.CourseIds)  
  6.             {  
  7.                 db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });  
  8.                 db.SaveChanges();  
  9.             }  
  10.         
  11.             return View();  
  12. my(custom model) view model using for that  
  13.  public class EditEmployeeVm  
  14.     {  
  15.         public int Id { setget; }  
  16.         public List<SelectListItem> Courses { getset; }  
  17.         public int[] CourseIds { setget; }  
  18.         public List<CourseVm> ExistingCourses { setget; }  
  19.     }  
  20.     public class CourseVm  
  21.     {  
  22.         public int Id { setget; }  
  23.         public string Name { setget; }  
  24.     }  
  25. }  
 to show what i need clearly see image below
 

Attachment: jquery_remove.rar

Answers (6)