public ActionResult Edit(CourseTopicViewModel edit) { if (ModelState.IsValid) { var check = _context.Topics.Include(c => c.Course).Where(c => c.Name == edit.Topic.Name && c.CourseID == edit.Topic.CourseID); if (check.Count() > 0) { ModelState.AddModelError("Name", "Topic Already Exists."); } else { var toppicInDb = _context.Topics.Find(edit.Topic.ID); toppicInDb.Name = edit.Topic.Name; toppicInDb.Description = edit.Topic.Description; _context.SaveChanges(); return(RedirectToAction("Index")); } } var topicVM = new CourseTopicViewModel() { Courses = _context.Courses.ToList(), Topic = edit.Topic, }; return(View(topicVM)); }
public ActionResult Create(CourseTopicViewModel topcourse) { if (ModelState.IsValid) { var check = _context.Topics.Include(c => c.Course).Where(c => c.Name == topcourse.Topic.Name && c.CourseID == topcourse.Topic.CourseID); if (check.Count() > 0) //list ID comparison, if count == 0. jump to else { ModelState.AddModelError("", "Topic Already Exists."); } else { _context.Topics.Add(topcourse.Topic); _context.SaveChanges(); return(RedirectToAction("Index")); } } var topicVM = new CourseTopicViewModel() { Courses = _context.Courses.ToList(), Topic = topcourse.Topic, }; return(View(topicVM)); }
public ActionResult Create() { var viewModel = new CourseTopicViewModel { Courses = _context.Courses.ToList(), Topic = new Topic() }; return(View(viewModel)); }
public ActionResult Edit(int id) { var topicInDb = _context.Topics.SingleOrDefault(t => t.ID == id); if (topicInDb == null) { return(HttpNotFound()); } var viewModel = new CourseTopicViewModel { Topic = topicInDb, Courses = _context.Courses.ToList() }; return(View(viewModel)); }