I want to update the balance of the quantity after the quantity is being chosen by the user. I refer the previous question in
After user choose the quantity, and then it will save into database and also update the balance in inventory.
The code is the same with the code in previous questions but there is some changes that i did on controller to update the balance in inventory.
[HttpPost]
public ActionResult AddMaterial(ItemModel model, string id)
{
TempData["Selected"] = "1";
id = Convert.ToString(TempData["NewID"]);
try
{
//// to save the dropdown details into database, table Material_Item//////
Material_Item item = db.Material_Item.Where(x => x.item_id == model.item_id).FirstOrDefault();
item = new Material_Item();
item.item_id = model.item_id;
item.Requester_id = id;
item.req_quantity = model.req_quantity;
item.req_material = model.req_material;
var reqMat = Convert.ToInt32(item.req_quantity);
///// to update the balance after user choose the quantity into database, table MaterialList
MaterialList list = db.MaterialLists.Where(y => y.Material == model.req_material).FirstOrDefault();
list = new MaterialList();
list.Mat_Quantity = model.Mat_Quantity - reqMat;
db.Material_Item.Add(item);
db.MaterialLists.Add(list);
db.SaveChanges();
}
catch (Exception)
{
}
return RedirectToAction("FormRequest", "Home");
}