Exemplo n.º 1
0
        public ActionResult Edit(CourseIdWithCourses EditedEntry)
        {
            var userEntryFromDB = context.Students.Find(EditedEntry.Id);

            userEntryFromDB.StudentName      = EditedEntry.StudentName;
            userEntryFromDB.StudentRoll      = EditedEntry.StudentRoll;
            userEntryFromDB.StudentDeprtment = EditedEntry.StudentDeprtment;
            userEntryFromDB.StudentBatch     = EditedEntry.StudentBatch;

            context.Entry(userEntryFromDB).State = EntityState.Modified;
            context.SaveChanges();


            foreach (var DeletedItem in userEntryFromDB.StudentWithCourses.ToList())
            {
                context.StudentWithCourses.Remove(DeletedItem);
                context.SaveChanges();
            }


            foreach (var courseId in EditedEntry.CourseIdList)
            {
                var EditedCourses = new StudentWithCourses()
                {
                    CourseId  = courseId,
                    StudentId = userEntryFromDB.Id,
                };
                context.StudentWithCourses.Add(EditedCourses);
                context.SaveChanges();
            }

            return(RedirectToAction("ShowAllStudent"));
        }
Exemplo n.º 2
0
        public ActionResult Index(CourseIdWithCourses student)
        {
            var StudentWithCourses = new Student()
            {
                StudentName      = student.StudentName,
                StudentRoll      = student.StudentRoll,
                StudentBatch     = student.StudentBatch,
                StudentDeprtment = student.StudentDeprtment
            };

            context.Students.Add(StudentWithCourses);
            context.SaveChanges();
            foreach (var item in student.CourseIdList)
            {
                var newStudentCourse = new StudentWithCourses()
                {
                    StudentId = StudentWithCourses.Id,
                    CourseId  = item,
                };
                context.StudentWithCourses.Add(newStudentCourse);
                context.SaveChanges();
            }
            return(RedirectToAction("Index"));
        }