public IActionResult Upsert(int?id) { StockItemVM stockItemVM = new StockItemVM() { StockItem = new Models.StockItem(), EmployeeList = _unitOfWork.Employee.GetAll().Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }), WareHouseList = _unitOfWork.WareHouse.GetAll().Select(i => new SelectListItem { Text = i.Title, Value = i.Id.ToString() }) }; if (id == null) { return(View(stockItemVM)); } stockItemVM.StockItem = _unitOfWork.StockItem.Get(id.GetValueOrDefault()); if (stockItemVM.StockItem == null) { return(NotFound()); } return(View(stockItemVM)); }
public IActionResult EditStockItem(int id, StockItemVM vm) { if (vm == null) { throw new Exception("Model cannot be empty"); } _updateItem.UpdateStockItemVM(vm); return(Json(new { materialId = vm.Id })); }
public void UpdateStockItemVM(StockItemVM StockItems) { var DBrecord = _context.StockItems.Find(StockItems.Id); if (StockItems != null) { DBrecord.ItemName = StockItems.ItemName == null || StockItems.ItemName == "" ? DBrecord.ItemName : StockItems.ItemName; DBrecord.Description = StockItems.Description == null || StockItems.Description == "" ? DBrecord.Description : StockItems.Description; DBrecord.Quantity = StockItems.Quantity; } try { _context.SaveChanges(); } catch (Exception e) { var errorMsg = e.ToString(); } }
public IActionResult Upsert(StockItemVM stockItemVM) { if (ModelState.IsValid) { string webRootPath = _hostEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; if (files.Count > 0) { string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"images\items"); var extension = Path.GetExtension(files[0].FileName); if (stockItemVM.StockItem.ImageUrl != null) { //this is an edit we have to remove old image var imagePath = Path.Combine(webRootPath, stockItemVM.StockItem.ImageUrl.Trim('\\')); if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } } using (var fileStreams = new FileStream(Path.Combine(uploads, fileName + extension), FileMode.Create)) { files[0].CopyTo(fileStreams); } stockItemVM.StockItem.ImageUrl = @"\images\items\" + fileName + extension; } else { //update when they don't change the image if (stockItemVM.StockItem.Id != 0) { StockItem objFromDb = _unitOfWork.StockItem.Get(stockItemVM.StockItem.Id); stockItemVM.StockItem.ImageUrl = objFromDb.ImageUrl; } } if (stockItemVM.StockItem.Id == 0) { _unitOfWork.StockItem.Add(stockItemVM.StockItem); } else { _unitOfWork.StockItem.Update(stockItemVM.StockItem); } _unitOfWork.Save(); return(RedirectToAction(nameof(Index))); } else { stockItemVM.EmployeeList = _unitOfWork.Employee.GetAll().Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }); stockItemVM.WareHouseList = _unitOfWork.WareHouse.GetAll().Select(i => new SelectListItem { Text = i.Title, Value = i.Id.ToString() }); if (stockItemVM.StockItem.Id != 0) { stockItemVM.StockItem = _unitOfWork.StockItem.Get(stockItemVM.StockItem.Id); } } return(View(stockItemVM.StockItem)); }