public ActionResult Edit(int?id, IEnumerable <int> roomNames) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Request request = db.Requests.Find(id); request.statusID = 3; if (request == null) { return(HttpNotFound()); } //Updates the room with the correct set of facilities, either remove or add is allowed Request postAttached = db.Requests.Where(x => x.requestID == id).First(); request.RoomRequests = postAttached.RoomRequests; request.RoomRequests.Clear(); foreach (var i in roomNames) { RoomRequest temp = new RoomRequest() { roomID = db.Rooms.Where(a => a.roomID == i).Select(b => b.roomID).First(), groupSize = 42 }; request.RoomRequests.Add(temp); } //Updates old version of room with edited values, then saves to database db.Entry(postAttached).CurrentValues.SetValues(request); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "facilityID,facilityName")] Facility facility) { var fac = db.Facilities.Where(a => a.facilityID == facility.facilityID).Select(a => a.facilityName); ViewBag.error = ""; if (!checkDuplicate(facility.facilityName) && fac.First() != facility.facilityName) { ViewBag.error = "Facility already exists"; return(View(facility)); } if (ModelState.IsValid && (checkDuplicate(facility.facilityName) || fac.First() == facility.facilityName)) { Facility postAttached = db.Facilities.Where(x => x.facilityID == facility.facilityID).First(); //Updates old version of room with edited values, then saves to database db.Entry(postAttached).CurrentValues.SetValues(facility); //db.Entry(facility).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(facility)); }
[ValidateAntiForgeryToken] //Editing everything works, not complete with error checking public ActionResult Edit([Bind(Include = "roomID,roomCode,buildingID,capacity")] Room room, bool Labe, IEnumerable <int> fac) { ViewBag.error = ""; if (room.roomCode == null || room.roomCode.IndexOf(".") == -1) { ViewBag.error = "Room code not given"; var options = db.Buildings.AsEnumerable().Select(s => new { buildingID = s.buildingID, Info = string.Format("{0} - {1}", s.buildingCode, s.buildingName) }); var facilityNames = db.Facilities.ToList(); ViewBag.facilities = facilityNames; ViewBag.buildingID = new SelectList(options, "buildingID", "Info"); return(View(room)); } room.roomCode = roomCodeToUpper(room.roomCode); var bID = room.buildingID; var bCode = room.roomCode; bCode = bCode.Substring(0, bCode.IndexOf(".")); var oldRoomCode = db.Rooms.Where(x => x.roomID == room.roomID).Select(x => x.roomCode).First(); var newRoomCode = room.roomCode; //Get all roomID where the roomName is the same as the one the user inputted var roomings = from roomDB in db.Rooms where roomDB.roomCode == newRoomCode select roomDB.roomCode; var result = db.Buildings.Where(s => s.buildingCode.Contains(bCode)).Select(s => s.buildingID); if (result.First() != bID) { ViewBag.error = "Building code doesn't match code stated in room"; } else if (!checkRoomCode(room.roomCode)) { ViewBag.error = "Room code not in right format"; } else if (!validate(room.roomCode) && oldRoomCode != newRoomCode) { ViewBag.error = "Room code already exists"; } else if (room.capacity < 1 || room.capacity > 400) { ViewBag.error = "Room capacity exceeds range. Capacity limit is 1 - 400."; } else if (result.First() == room.buildingID && (roomings.Count() == 0 || oldRoomCode == newRoomCode) && checkRoomCode(room.roomCode) && (room.capacity >= 1 && room.capacity <= 400)) { if (Labe) { room.lab = 1; } else { room.lab = 0; } if (ModelState.IsValid) { //Updates the room with the correct set of facilities, either remove or add is allowed Room postAttached = db.Rooms.Where(x => x.roomID == room.roomID).First(); room.Facilities = postAttached.Facilities; room.Facilities.Clear(); if (fac != null) { foreach (int f in fac) { room.Facilities.Add(db.Facilities.Where(x => x.facilityID == f).First()); } } //Updates old version of room with edited values, then saves to database db.Entry(postAttached).CurrentValues.SetValues(room); db.SaveChanges(); return(RedirectToAction("Index")); } } var selected = db.Rooms.Where(a => a.roomID == room.roomID).Select(a => a.Facilities.Select(c => c.facilityID)).ToList(); ViewBag.selectedFac = selected[0]; var facilityNames2 = db.Facilities.ToList(); ViewBag.facilities = facilityNames2; var options2 = db.Buildings.AsEnumerable().Select(s => new { buildingID = s.buildingID, Info = string.Format("{0} - {1}", s.buildingCode, s.buildingName) }); ViewBag.buildingID = new SelectList(options2, "buildingID", "Info"); return(View(room)); }