public ActionResult EditMaterialInStock(EditMaterialInStockViewModel model) { try { if (ModelState.IsValid) { var materialInStock = db.MaterialInStocks.Where(r => r.MaterialId == model.MaterialId && r.StockId == model.StockId).FirstOrDefault(); if (materialInStock != null) { // get quantity var quantity = 0; if (!int.TryParse(model.Quantity.Replace(",", ""), out quantity)) { throw new Exception("Số lượng tồn không hợp lệ!"); } materialInStock.Quantity = quantity; db.SaveChanges(); return(RedirectToAction("MaterialInstock", "Material")); } else { ModelState.AddModelError("", "Không tìm thấy thông tin tồn kho!!!"); } } } catch (Exception ex) { ModelState.AddModelError("", ex); } // assign view bag values try { var materials = db.Materials.OrderBy(t => t.MaterialName) .AsEnumerable() .Select(t => new SelectListItem { Value = t.MaterialId.ToString(), Text = t.MaterialName, Selected = t.MaterialId == model.MaterialId }) .ToList(); var stocks = db.Stocks.OrderBy(r => r.StockName) .AsEnumerable() .Select(r => new SelectListItem { Value = r.StockId.ToString(), Text = r.StockName, Selected = r.StockId == model.StockId }) .ToList(); ViewBag.Materials = materials; ViewBag.Stocks = stocks; } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } return(View()); }
public ActionResult EditMaterialInStock(int materialId = 0, int stockId = 0) { var model = new EditMaterialInStockViewModel(); try { // get material in stock var materialInStock = db.MaterialInStocks.Where(r => r.MaterialId == materialId && r.StockId == stockId).FirstOrDefault(); if (materialInStock != null) { model.MaterialId = materialInStock.MaterialId; var material = db.Materials.Where(r => r.MaterialId == materialInStock.MaterialId).FirstOrDefault(); if (material != null) { model.MaterialName = material.MaterialName; } model.StockId = materialInStock.StockId; var stock = db.Stocks.Where(r => r.StockId == materialInStock.StockId).FirstOrDefault(); if (stock != null) { model.StockName = stock.StockName; } model.Quantity = materialInStock.Quantity.ToString(); } else { ModelState.AddModelError("", "Không tìm thấy thông tin tồn kho!!!"); } } catch (Exception ex) { ModelState.AddModelError("", ex); } // assign view bag values try { var materials = db.Materials.OrderBy(t => t.MaterialName) .AsEnumerable() .Select(t => new SelectListItem { Value = t.MaterialId.ToString(), Text = t.MaterialName, Selected = t.MaterialId == materialId }) .ToList(); var stocks = db.Stocks.OrderBy(r => r.StockName) .AsEnumerable() .Select(r => new SelectListItem { Value = r.StockId.ToString(), Text = r.StockName, Selected = r.StockId == stockId }) .ToList(); ViewBag.Materials = materials; ViewBag.Stocks = stocks; } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } return(View(model)); }