CANNOT SAVE CHANGES OF COURSES IF REMOVE IN EDIT HTTPPOST

Abeer Shlby

Total Post:17

Points:119
Posted by  Abeer Shlby
 678  View(s)
Ratings:
Rate this:

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 image describe what i need as following


 link to my code to all project as following

http://www.mediafire.com/download/3xs35gecmvkksj1/all_project.txt
code 

my code as following 

    [HttpPost]  
            public ActionResult Edit(EditEmployeeVm model)  
            {  
                var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);  
                foreach (var couseid in model.CourseIds)  
                {  
                    db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });  
                    db.SaveChanges();  
                }  
            
                return View();  
    my(custom model) view model using for that  
     public class EditEmployeeVm  
        {  
            public int Id { setget; }  
            public List<SelectListItem> Courses { getset; }  
            public int[] CourseIds { setget; }  
            public List<CourseVm> ExistingCourses { setget; }  
        }  
        public class CourseVm  
        {  
            public int Id { setget; }  
            public string Name { setget; }  
        }  
    }  


    Answer

    NEWSLETTER

    Enter your email address here always to be updated. We promise not to spam!