how to delete assigned courses only not all course

2019-07-27 02:06发布

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");
            });

0条回答
登录 后发表回答