public ActionResult MilkEdit(MilkProductionView newentry, string Submit) { string name = HttpContext.User.Identity.Name; string operatorName = AgroExpressDBAccess.GetFullNamebyUserID(name); if (Submit == "Add") { if (ModelState.IsValid) { List <MilkProductionPerCow> tem = newentry.Cows; List <MilkProduction> entryList = new List <MilkProduction>(); var totalmilk = 0.0; foreach (var entry in tem) { if (entry.MorningAmount != 0 && entry.AfternoonAmount != 0) { entryList.Add(new MilkProduction { AnimalId = entry.AnimalID, Date = newentry.ProductionDate, AfternoonAmount = entry.AfternoonAmount, MorningAmount = entry.MorningAmount, OperatorName = operatorName }); totalmilk += entry.AfternoonAmount + entry.MorningAmount; } } if (totalmilk != 0) { if (AnimalInformationDBAcces.AddOrUpdateMilkProduction(entryList)) { if (AgroExpressDBAccess.AddOrUpdateTotalMilkSummary(newentry.ProductionDate, totalmilk)) { ViewBag.success = "Update Successful"; return(View(newentry)); } } } else { ViewBag.success = "Update Failed, Total Value Can not be Zero"; return(View(newentry)); } } return(View(newentry)); } else { ModelState.Clear(); List <MilkProduction> prevmilkproductionlist = AnimalInformationDBAcces.GetallMilkProductionByDate(newentry.ProductionDate);; prevmilkproductionlist = prevmilkproductionlist.Where(a => a.animal.AnimalTypeId == newentry.AnimalTypeId).ToList(); MilkProductionView tem = new MilkProductionView(); tem.ProductionDate = newentry.ProductionDate; List <MilkProductionPerCow> productionList = new List <MilkProductionPerCow>(); if (prevmilkproductionlist.Count != 0) { foreach (var animal in prevmilkproductionlist) { productionList.Add(new MilkProductionPerCow { AnimalID = animal.AnimalId, AnimalCode = animal.animal.AnimalCodeName, AfternoonAmount = animal.AfternoonAmount, MorningAmount = animal.MorningAmount }); } } tem.Cows = productionList; tem.AnimalTypeId = newentry.AnimalTypeId; return(View(tem)); } }