public ActionResult Create(DeviceCreateViewModel deviceCreateViewModel) { if (ModelState.IsValid) { Device device = new Device { AttachedPin = deviceCreateViewModel.AttachedPin, Name = deviceCreateViewModel.Name, RoomId = deviceCreateViewModel.RoomId, State = deviceCreateViewModel.State }; string ReceiverIp = this.Data.Rooms.All() .Where(r => r.Id == deviceCreateViewModel.RoomId) .Select(s => s.Floor.House.ReceiverIp) .SingleOrDefault(); if (ReceiverIp == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } DeviceRCModel deviceRCModel = new DeviceRCModel { ReceiverIp = ReceiverIp, Pin = deviceCreateViewModel.AttachedPin, State = deviceCreateViewModel.State, }; using (TransactionScope transaction = new TransactionScope()) { this.Data.Devices.Add(device); this.Data.SaveChanges(); this.RemoteControl.SendDeviceSettings(deviceRCModel); transaction.Complete(); } return RedirectToAction("RoomDetails", "Rooms", new { RoomId = device.RoomId }); } return View(deviceCreateViewModel); }
public ActionResult Create(int roomId) { var houseId = this.Data.Rooms.All() .Where(r => r.Id == roomId) .Select(r => r.Floor.HouseId) .SingleOrDefault(); if (houseId == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var allowedPin = HelperClass.GetAllowedPins(this.Data, houseId, null); if (!(allowedPin.Count() > 0)) { return PartialView("_ErrorMessagePartial", "Тhere are no free pins"); } DeviceCreateViewModel deviceCreateViewModel = new DeviceCreateViewModel { AllowedPins = new SelectList(allowedPin), RoomId = roomId }; return View(deviceCreateViewModel); }