使用foreach保存数据不能正确保存课程(删除以前的课程

时间:2016-09-03 12:42:27

标签: entity-framework asp.net-mvc-4 linq-to-entities entity-framework-6

需要

在员工表中保存姓名并保存所有课程

在编辑[HTTPPOST]

中单击提交按钮时,在员工课程表中

问题摘要

提交按钮仅保存更改并删除数据

之前

问题详情

在编辑帖子中点击提交按钮。课程将保存

我在点击提交之前添加或选择的课程将保存

但员工以前的课程将删除

示例

1-我添加员工姓名MEDO有课程c ++

2 - 在编辑视图中显示c ++

3 - 如果我在编辑视图中添加python然后单击提交它必须有两个课程

c ++和python

4-只有在再次打开该员工的编辑时才向我展示

它保存所选课程,但课程在删除之前存在

问题图片

下面的图片显示了所有细节

SAVE all courses in employeecourse table

代码

public class EditEmployeeVm
{
    public int Id { set; get; }
    public string Name { get; set; }
    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; }
}
in edit function get i pass data to edit view

    public ActionResult Edit(int id)
    {
     var vm = new EditEmployeeVm { Id = id };
            var emp = db.Employees.FirstOrDefault(f => f.Id == id);
            vm.Name = emp.Name;
            vm.ExistingCourses = db.EmployeeCourses
                                    .Where(g => g.EmployeeId == id)
                                    .Select(f => new CourseVm
                                    {
                                        Id = f.Id,
                                        Name = f.Course.CourseName
                                    }).ToList();

            vm.CourseIds = vm.ExistingCourses.Select(g => g.Id).ToArray();
            vm.Courses = db.Courses.Select(f => new SelectListItem
            {
                Value = f.Id.ToString(),
                Text = f.CourseName
            }).ToList();

            return View(vm);
    }



[HttpPost]
        public ActionResult Edit(EditEmployeeVm model)
        {
            var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);
            foreach (EmployeeCourse eec in emp.EmployeeCourses.ToList())
            {
                var ec = db.EmployeeCourses.Find(eec.Id);
                db.EmployeeCourses.Remove(ec);
                db.SaveChanges();
            }

            foreach (var couseid in model.CourseIds)
            {
                db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
                db.SaveChanges();
            }

            return View();
        }

更新

 [HttpPost]
        public ActionResult Edit(EditEmployeeVm model)
        {
    **//i need to save name employee changes in employee table and all courses in emloyeecourse table
    //what is wrong here**
       var emp = db.Employees
                   .Include(e => e.EmployeeCourses)
                   .FirstOrDefault(f => f.Id == model.Id);

            foreach (var eec in emp.EmployeeCourses.ToList())
            {
                db.EmployeeCourses.Remove(eec);
            }

            foreach (var couseid in model.CourseIds)
            {
                db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
            }

            db.SaveChanges();

            return View();
        }  

1 个答案:

答案 0 :(得分:0)

您可以尝试如下所示。

注意:您必须使用Include来抓取EmployeeCourses,如下所示。

    [HttpPost]
    public ActionResult Edit(EditEmployeeVm model)
    {

       var emp = db.Employees
                   .Include(e => e.EmployeeCourses)
                   .FirstOrDefault(f => f.Id == model.Id);

            foreach (var eec in emp.EmployeeCourses.ToList())
            {
                db.EmployeeCourses.Remove(eec);
            }

          db.SaveChanges();

            foreach (var couseid in model.CourseIds)
            {
                db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
            }

            db.SaveChanges();

            return View();
    }