// // GET: /CenterFreeResources/Edit public ActionResult Edit() { int centerid = account.GetCurrentUserCenterId(); ResourceViewModelsEdit resourcesEdit = new ResourceViewModelsEdit(); List<CenterFreeResource> resourceList = repoService.centerResourcesRepo.FindAllWithCenterId(centerid).ToList(); List<ResourceViewModel> resouceViewModelList = new List<ResourceViewModel>(); foreach (var resource in resourceList) { ResourceViewModel r = new ResourceViewModel(); r.resource = resource; r.count = (int)resource.NumberAvailable; List<GivenResource> recordsOfGiven = resource.GivenResources.ToList(); foreach (var record in recordsOfGiven) { r.totalHandedOut += record.Count; } resouceViewModelList.Add(r); } resourcesEdit.Resources = resouceViewModelList; ViewBag.CurrentPage = "Resources"; return View(resourcesEdit); }
public ActionResult Edit(ResourceViewModelsEdit resourcesEdit) { foreach (var item in resourcesEdit.Resources) { if (ModelState.IsValid) { int handedOut = (int)(item.resource.NumberAvailable) - item.count; int newStock = item.count; //this if block allows the user to enter a negative number to indicate //number handed out instead of current stock if (item.count < 0) { handedOut = -item.count; if((int)(item.resource.NumberAvailable) - handedOut < 0) handedOut = (int)(item.resource.NumberAvailable); newStock = (int)(item.resource.NumberAvailable) - handedOut; } CenterFreeResource aResource = repoService.centerResourcesRepo.FindById(item.resource.Id); aResource.NumberAvailable = newStock; repoService.centerResourcesRepo.Update(aResource); if (handedOut > 0) { GivenResource gr = new GivenResource(); gr.Count = handedOut; gr.CenterFreeResourceId = item.resource.Id; repoService.centerResourcesRepo.Insert(gr); } } } return RedirectToAction("Index"); }