public ActionResult Create(DeviceLocationViewModel _viewModel) { if (ModelState.IsValid) { var _model = GetModelDatas(_viewModel); db.DeviceLocations.Add(_model); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "InventoryNumber", _viewModel.DeviceID); return(View(_viewModel)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } DeviceLocation _model = db.DeviceLocations.Find(id); var _viewModel = DeviceLocationViewModel.GetViewModelDatas(_model); if (_model == null) { return(HttpNotFound()); } ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "InventoryNumber", _model.DeviceID); return(View(_viewModel)); }
DeviceLocation GetModelDatas(DeviceLocationViewModel _viewModel) { DeviceLocation _model = new DeviceLocation() { LocationID = _viewModel.LocationID, DeviceID = _viewModel.DeviceID, DepartmentLoc = _viewModel.DepartmentLoc.ToString(), CorpLoc = _viewModel.CorpLoc.ToString(), FloorLoc = _viewModel.FloorLoc.ToString(), RoomLoc = _viewModel.RoomLoc, NoteDevLoc = _viewModel.NoteDevLoc, CreateDateTime = _viewModel.CreateDateTime, ChangeDateTime = _viewModel.ChangeDateTime }; return(_model); }
// GET: DeviceLocations public ActionResult Index(string searchString, int?page) { #region Default Code //var deviceLocations = db.DeviceLocations.Include(d => d.Device); //return View(deviceLocations.ToList()); #endregion // end of Default Code #region LINQ Expression //var qData = from q in db.Devices.Include(d => d.DeviceDocument) select q; var qData = (from q in db.DeviceLocations.Include(d => d.Device) select q).OrderBy(s => s.FloorLoc).Skip(0); if (!string.IsNullOrEmpty(searchString)) { qData = qData.Where(s => s.FloorLoc.Contains(searchString)); } int pageSize = 8; int pageNumber = (page ?? 1); var _viewModel = new List <DeviceLocationViewModel>(); foreach (var item in qData) { _viewModel.Add(DeviceLocationViewModel.GetViewModelDatas(item)); } return(View("Index", _viewModel.ToPagedList(pageNumber, pageSize))); //return View(_listViewModel.ToPagedList(pageNumber, pageSize)); #endregion // end of LINQ Expression #region List //var _model = db.Devices.Include(d => d.DeviceDocument); //var _viewModel = new List<DeviceLocationViewModel>(); //foreach (var item in _model) //{ // _viewModel.Add(DeviceLocationViewModel.GetViewModelDatas(item)); //} //return View(_viewModel); #endregion // end of List }
public ActionResult Edit(DeviceLocationViewModel _viewModel) { if (ModelState.IsValid) { var _model = GetModelDatas(_viewModel); try { db.Entry(_model).State = EntityState.Modified; db.SaveChanges(); } catch (DbEntityValidationException ex) { throw new DbEntityValidationException(ex.ToString()); } return(RedirectToAction("Index")); } ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "InventoryNumber", _viewModel.DeviceID); return(View(_viewModel)); }