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
-
- <script>
- $(function () {
- $(document).on("click", ".remove", function (e) {
- e.preventDefault();
- $(this).closest(".course-item").remove();
- });
- $('#AvailableCourses').change(function () {
-
- var existingCourses = $("input[name='CourseIds']")
- .map(function () { return this.value; }).get();
- }
- });
- });
- </script>
-
-
- [HttpPost]
- public ActionResult Edit(EditEmployeeVm model)
-
- {
- var origunalCourseIds = TempData["OriginalId"] as int[];
- var newCourceIds = model.CourseIds;
- foreach (var item in origunalCourseIds)
- {
- if (newCourceIds.Any(m => m == item))
- {
-
- var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);
- foreach (var couseid in newCourceIds)
- {
- db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
- db.SaveChanges();
-
- }
-
- }
- **else
- {
- **
- var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);
- foreach (var couseid in newCourceIds)
- {
- db.EmployeeCourses.Remove(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
- db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
- db.SaveChanges();
-
- }
-
-
- }
- }**
- return View();
- }
-
-
- public class EditEmployeeVm
- {
- public int Id { set; get; }
- public List<SelectListItem> Courses { get; set; }
- public int[] CourseIds { set; get; }
- public List<CourseVm> ExistingCourses { set; get; }
- }
- public class CourseVm
- {
- public int Id { set; get; }
- public string Name { set; get; }
- }
- }
-
- public ActionResult Edit(int id)
- {
-
- var vm = new EditEmployeeVm { Id = id };
-
-
- vm.CourseIds = vm.ExistingCourses.Select(g => g.Id).ToArray();
- TempData["OriginalId"] = vm.CourseIds;
- return View(vm);
- }