public ActionResult Create(AnimalWeightView model) { if (ModelState.IsValid) { ViewBag.AnimalId = new SelectList(_animalBl.GetAllAnimals(), "AnimalId", "AnimalId"); _weight.AddAnimalWeight(model); return RedirectToAction("GetAll"); } return View(model); }
//weigh animal public void AddAnimalWeight(AnimalWeightView model) { using (var animalweight = new AnimalWeightRepository()) { var weight = new AnimalWeight { AnimalWeightId = model.AnimalWeightId, DateWeighted = DateTime.Now, OriginalWeight = model.OriginalWeight, AverageWeight = model.AverageWeight, AnimalId = model.AnimalId }; List<AnimalWeight> listAni = animalweight.GetAll().ToList().FindAll(x => x.AnimalId == model.AnimalId).ToList(); weight.AverageWeight += ((model.OriginalWeight+listAni.Sum(x => x.OriginalWeight)) / (listAni.Count+1)); animalweight.Insert(weight); } }