コード例 #1
0
 public ActionResult Create(SchoolsViewModel vm)
 {
     if (Session["isAdmin"] == null || !(bool)Session["isAdmin"])
         return RedirectToAction("Index", new { controller = "Home", action = "Index" });
     if (ModelState.IsValid)
     {
         var existingSchool = db.Schools.Where(school => school.SchoolName == vm.SchoolModel.SchoolName);
         var existingLabel = db.Schools.Where(school => school.LabelId == vm.SchoolModel.LabelId);
         if (existingSchool.ToList().Count > 0)
         {
             ModelState.AddModelError("", "School Name already exists");
             vm.Labels = createLabelSelectList();
             return View(vm);
         }
         else if (existingLabel.ToList().Count > 0)
         {
             ModelState.AddModelError("", "Another School is already associated with the selected label");
             vm.Labels = createLabelSelectList();
             return View(vm);
         }
         db.Schools.Add(vm.SchoolModel);
         db.SaveChanges();
     }
     return RedirectToAction("Index");
 }
コード例 #2
0
 // GET: Schools/Create
 public ActionResult Create()
 {
     if (Session["isAdmin"] == null || !(bool)Session["isAdmin"])
         return RedirectToAction("Index", new { controller = "Home", action = "Index" });
     SchoolsViewModel vm = new SchoolsViewModel
     {
         SchoolModel = new Schools(),
         Labels = createLabelSelectList()
     };
     return View(vm);
 }
コード例 #3
0
 // GET: Schools/Edit/5
 public ActionResult Edit(int? id)
 {
     if (Session["isAdmin"] == null || !(bool)Session["isAdmin"])
         return RedirectToAction("Index", new { controller = "Home", action = "Index" });
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     Schools schools = db.Schools.Find(id);
     if (schools == null)
     {
         return HttpNotFound();
     }
     SchoolsViewModel vm = new SchoolsViewModel
     {
         SchoolModel = schools,
         Labels = createLabelSelectList()
     };
     return View(vm);
 }