Skip to content
Loading
  • foreach(varcinModel.ExistingCourses)
  • {
  • class="course-item">
  • @c.Name"#"class="remove"data-id="@c.Id">Remove
  • "text"name="CourseIds"value="@c.Id"/>
  • }
  • }
  • 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
    0
  • 0
    can you give me code for remove in controller and jquery if possible
    0