public ActionResult Create(Server server) { if (ModelState.IsValid) { try { var namecheck = db.Servers.Where(s => s.ServerName == server.ServerName).Count(); if (namecheck == 0) { db.Servers.Add(server); db.SaveChanges(); return RedirectToAction("Index"); } if (namecheck > 0) { ViewBag.Message = "A Server Name Already Exists. Please user another Name"; ViewBag.RackID = new SelectList(db.Racks, "RackID", "RackName", server.RackID); ViewBag.HardWareModelID = new SelectList(db.HardWareModels, "HardWareModelID", "HardwareModelName", server.HardWareModelID); ViewBag.VendorID = new SelectList(db.Vendors, "VendorID", "VendorName", server.VendorID); ViewBag.AssetTagID = new SelectList(db.AssetTags, "AssetTagID", "AssetTagName", server.AssetTagID); ViewBag.NetworkSwitchID = new SelectList(db.NetworkSwitchs, "NetworkSwitchID", "NetworkSwitchName", server.NetworkSwitchID); ViewBag.ServiceRoleID = new SelectList(db.ServiceRoles, "ServiceRoleID", "ServiceRoleName", server.ServiceRoleID); } } catch { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } } return View(server); }
public ActionResult Edit(Server server) { if (ModelState.IsValid) { db.Entry(server).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.RackID = new SelectList(db.Racks, "RackID", "RackName", server.RackID); ViewBag.HardWareModelID = new SelectList(db.HardWareModels, "HardWareModelID", "HardwareModelName", server.HardWareModelID); ViewBag.VendorID = new SelectList(db.Vendors, "VendorID", "VendorName", server.VendorID); ViewBag.AssetTagID = new SelectList(db.AssetTags, "AssetTagID", "AssetTagName", server.AssetTagID); ViewBag.NetworkSwitchID = new SelectList(db.NetworkSwitchs, "NetworkSwitchID", "NetworkSwitchName", server.NetworkSwitchID); ViewBag.ServiceRoleID = new SelectList(db.ServiceRoles, "ServiceRoleID", "ServiceRoleName", server.ServiceRoleID); return View(server); }