Skip to content
Loading
cannot save changes of courses if remove in edit httppost
  • }
  • }
  • in edit get of view of controller i using CourseIds variable as following
    1. publicActionResultEdit(intid)
    2. {
    3. varvm=newEditEmployeeVm{Id=id};
    4. varemp=db.Employees.FirstOrDefault(f=>f.Id==id);
    5. vm.Name=emp.Name;
    6. vm.ExistingCourses=db.EmployeeCourses
    7. .Where(g=>g.EmployeeId==id)
    8. .Select(f=>newCourseVm
    9. {
    10. Id=f.Id,
    11. Name=f.Course.CourseName
    12. }).ToList();
    13. vm.CourseIds=vm.ExistingCourses.Select(g=>g.Id).ToArray();
    14. vm.Courses=db.Courses.Select(f=>newSelectListItem
    15. {
    16. Value=f.Id.ToString(),
    17. Text=f.CourseName
    18. }).ToList();
    19. returnView(vm);
    20. }
    in edit httppost
     
    i defined CourseIds to using in add and update existing(remove) but i cannot do update to
     
    existing if i remove it only add courseid when add new course
     
    1. publicActionResultEdit(EditEmployeeVmmodel)
    2. {
    1. var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);
    2. foreach (var couseid in model.CourseIds)
    3. {
    4. db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
    5. db.SaveChanges();
    6. }
    7. Note:code abelow attached in file name allproject.txt as following
  • Midhun Tp
    Hi,
    Actually how are you saving course id's on submit click? Are you saving the course id's in any variable and updating that variable on removing courses? If you are using client side script for removing a html element, it won't be effected in server side unless you are saving it in some hidden fields or variables.
  • can any one help me in that
  • i dont have any error
    but data not saved when remove course
    i attach all code in file with my message
  • Bikesh Srivastava
    Hi,
    you are doing good , i think some little bit mistake is happening ,can you share your complete code because we are not getting what is error point.???