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
- // remove courses success by jquery
- // here i write code for save courses added or courses removed
- [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))
- {
- // when add new course then click submit save success in database
- 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
- {
- **//remove courses then click submit not save removed item**
- 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();
- }
- //custom model using to my code
- //save in employeecourse table
- public class EditEmployeeVm
- {
- public int Id { set; get; }
- public List
Courses { get; set; } - public int[] CourseIds { set; get; }
- public List
ExistingCourses { set; get; } - }
- public class CourseVm
- {
- public int Id { set; get; }
- public string Name { set; get; }
- }
- }
- //in Edit [HTTPGET] Retrieve courses detailes for employee i need to edit
- 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);
- }
Shuvojit HalderPosted Sep 13, 2016, 7:41 AM
ahmed salahPosted Sep 13, 2016, 6:27 AM
Delpin Susai RajPosted Sep 13, 2016, 4:10 AM
Delpin Susai RajPosted Sep 13, 2016, 4:10 AM
Delpin Susai RajPosted Sep 13, 2016, 4:09 AM