Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cannot save changes of courses if remove in edit httppost

Status
Not open for further replies.

ahm1985

Programmer
Dec 6, 2012
138
0
0
EG
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

Code:
 [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
course_wrong9_g7fd2c.jpg
 
 http://files.engineering.com/getfile.aspx?folder=2d3156db-1ccd-423c-9c86-d122572963d5&file=all_project.txt
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top