how to delete assigned courses only not all courses for employee

非 Y 不嫁゛ 提交于 2019-12-11 05:53:15

问题


Problem

In edit view when i click remove course then click save it remove all course not

only i selected for remove

Details

if i have courses a,b,c for employee name michel then remove course a

and click save it delete all courses b,c also deleted

I need actually delete assigned for remove only meaning remove a and remaining

b,c. see image below it show what i need clearly

model Cusomemp2

 public class Cusomemp2
    {
        public int Id { get; set; }//employee
        public string Name { get; set; }//employee
        public List<EmployeeCourse> empcourses { get; set; }//employeecourse

    }

in edit http post i need when click save button remove only assigned for delete

[HttpPost]
        public ActionResult Edit(Cusomemp2 custom)
        {

            var result = db.Employees
                 .Where(p => p.Id == custom.Id)
                 .Include(c => c.EmployeeCourses)
                 .FirstOrDefault();

            if (custom.empcourses.Any())
        {
//in this foreach remove courses(not success)
            foreach (var ec in result.EmployeeCourses.ToList())//to remove existing EmployeeCourses
            {
                db.EmployeeCourses.Remove(ec);
                db.SaveChanges();

            }
        }

            return View();
        }

when click remove course it give value 0 for hidden course

the following show list of courses in view model

            var index = 0;
            $("#CourseId").change(function () {

                var id = $(this).val();
                var txt = $("#CourseId option:selected").text();
                $("#tb").append("<tr><td><input type = 'hidden' name='empcourses[" + index + "].CourseId' value='" + id + "'/></td><td>" + txt + "</td><td><input type='button' value='remove' class='r'</td></tr>")

                index++;
            });
            $("#tb").on("click", ".r", function () {
                $(this).parent().parent().hide();
                $(this).parent().prev().prev().find("input").val("0");
            });

来源:https://stackoverflow.com/questions/39509938/how-to-delete-assigned-courses-only-not-all-courses-for-employee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!