public ActionResult Milk(int TypeID)
        {
            List <MilkProduction> prevmilkproductionlist = AnimalInformationDBAcces.GetallMilkProductionByDate(System.DateTime.Now.Date);;

            prevmilkproductionlist = prevmilkproductionlist.Where(a => a.animal.AnimalTypeId == TypeID).ToList();

            List <AnimalInformation> animallist = new List <AnimalInformation>();

            animallist = AnimalInformationDBAcces.GetEnabledFemaleAnimalByTypeId(TypeID);

            MilkProductionView tem = new MilkProductionView();

            tem.ProductionDate = System.DateTime.Now.Date;
            List <MilkProductionPerCow> productionList = new List <MilkProductionPerCow>();

            foreach (var animal in animallist)
            {
                var previnfo = prevmilkproductionlist.Where(a => a.AnimalId == animal.PKAnimalId).SingleOrDefault();
                if (previnfo != null)
                {
                    productionList.Add(new MilkProductionPerCow {
                        AnimalID = animal.PKAnimalId, AnimalCode = animal.AnimalCodeName, AfternoonAmount = previnfo.AfternoonAmount, MorningAmount = previnfo.MorningAmount
                    });
                }
                else
                {
                    productionList.Add(new MilkProductionPerCow {
                        AnimalID = animal.PKAnimalId, AnimalCode = animal.AnimalCodeName, AfternoonAmount = 0, MorningAmount = 0
                    });
                }
            }
            tem.Cows         = productionList;
            tem.AnimalTypeId = TypeID;
            return(View(tem));
        }
        public ActionResult Milk(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 = "Insert Successful";
                                return(View(newentry));
                            }
                        }
                    }
                    else
                    {
                        ViewBag.success = "Insert 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();

                List <AnimalInformation> animallist = new List <AnimalInformation>();

                animallist = AnimalInformationDBAcces.GetEnabledFemaleAnimalByTypeId(newentry.AnimalTypeId);

                MilkProductionView tem = new MilkProductionView();
                tem.ProductionDate = newentry.ProductionDate;
                List <MilkProductionPerCow> productionList = new List <MilkProductionPerCow>();
                foreach (var animal in animallist)
                {
                    var previnfo = prevmilkproductionlist.Where(a => a.AnimalId == animal.PKAnimalId).SingleOrDefault();
                    if (previnfo != null)
                    {
                        productionList.Add(new MilkProductionPerCow {
                            AnimalID = animal.PKAnimalId, AnimalCode = animal.AnimalCodeName, AfternoonAmount = previnfo.AfternoonAmount, MorningAmount = previnfo.MorningAmount
                        });
                    }
                    else
                    {
                        productionList.Add(new MilkProductionPerCow {
                            AnimalID = animal.PKAnimalId, AnimalCode = animal.AnimalCodeName, AfternoonAmount = 0, MorningAmount = 0
                        });
                    }
                }
                tem.Cows         = productionList;
                tem.AnimalTypeId = newentry.AnimalTypeId;
                return(View(tem));
            }
        }