public ActionResult Create(SubjectViewModel subjectViewModel) { try { // TODO: Add insert logic here if (ModelState.IsValid) { Subject subject = new Subject() { CreditHours = subjectViewModel.CreditHours, Group = subjectViewModel.Group, PrimaryBook = subjectViewModel.PrimaryBook, ReferenceBook1 = subjectViewModel.ReferenceBook1, ReferenceBook2 = subjectViewModel.ReferenceBook2, SubjectName = subjectViewModel.SubjectName, SubjectTeacher = subjectViewModel.SubjectTeacher, }; db.Subjects.Add(subject); db.SaveChanges(); return RedirectToAction("Index"); } return View(subjectViewModel); } catch { return View(); } }
public ActionResult Edit(int id, SubjectViewModel subjectViewModel) { try { // TODO: Add update logic here if (ModelState.IsValid) { Subject subject = new Subject() { SubjectId = id, CreditHours = subjectViewModel.CreditHours, Group = subjectViewModel.Group, PrimaryBook = subjectViewModel.PrimaryBook, ReferenceBook1 = subjectViewModel.ReferenceBook1, ReferenceBook2 = subjectViewModel.ReferenceBook2, SubjectName = subjectViewModel.SubjectName, SubjectTeacher = subjectViewModel.SubjectTeacher, }; if (subject == null) { return new HttpNotFoundResult(); } db.Entry(subject).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(subjectViewModel); } catch { return View(); } }
// // GET: /Subject/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Subject subject = db.Subjects.Find(id); SubjectViewModel subjectViewModel = new SubjectViewModel() { CreditHours = subject.CreditHours, Group = subject.Group, PrimaryBook = subject.PrimaryBook, ReferenceBook1 = subject.ReferenceBook1, ReferenceBook2 = subject.ReferenceBook2, SubjectName = subject.SubjectName, SubjectTeacher = subject.SubjectTeacher, }; if (subject == null) { return HttpNotFound(); } return View(subjectViewModel); }