1 Replies - 500 Views - Last Post: 11 September 2016 - 04:51 AM

#1 ahmedba  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 24-January 14

cannot save changes of courses if remove in edit httppost

Posted 10 September 2016 - 07:29 PM

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

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 { 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; }  
    }  
}  

to show what i need clearly see image below
attached with message

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: cannot save changes of courses if remove in edit httppost

#2 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: cannot save changes of courses if remove in edit httppost

Posted 11 September 2016 - 04:51 AM

Right now you are only using Add to add items to the database. You need to add a call to Remove in order to remove an item from the database. What I would do is create a new route for a delete action with an HttpPost and a view model specific to that action. The view model will send back in CourseIds the courses to be removed. Then when the remove action is run check if the id is in the db and remove it if it exists. Exists and Remove do not have the right parameters, it is up to you to add the right parameters.

        [HttpPost]  
        public ActionResult Remove(RemoveEmployeeVm model)  
        {  
            var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);  
            foreach (var couseid in model.CourseIds)  
            {  
                if (db.EmployeeCourses.Exists(courseid))
                {
                    db.EmployeeCourses.Remove(courseid);  
                    db.SaveChanges();  
                }
            }  
            return View();  
        }



And the RemoveEmployeeVm model
    public class RemoveEmployeeVm  
    {  
        public int Id { set; get; }  
        public List<SelectListItem> Courses { get; set; }  
        public int[] CourseIds { set; get; }  // This should hold a list of courses to be removed
        public List<CourseVm> ExistingCourses { set; get; }  
    }  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1