public ActionResult Create(int?Id) { EmpToCustMilkVM empToCustMilkVM = new EmpToCustMilkVM() { Date = System.DateTime.Now }; empToCustMilkVM.CustomerInfoList = db.CustomerInfo.ToList().Select(m => new SelectListItem() { Text = m.CustomerName, Value = m.Id.ToString() }).ToList(); empToCustMilkVM.EmployeeInfoList = db.EmployeeInfo.ToList().Select(m => new SelectListItem() { Text = m.Name, Value = m.Id.ToString() }).ToList(); if (Id != 0 && Id != null) { var existingCustomerMilkDetail = db.StaffGivenMilkToCustomer.FirstOrDefault(c => c.Id == Id); if (existingCustomerMilkDetail == null) { return(RedirectToAction("Index")); } else { empToCustMilkVM.CustomerId = existingCustomerMilkDetail.CustomerId; empToCustMilkVM.Date = existingCustomerMilkDetail.Date; empToCustMilkVM.MilkGiven = existingCustomerMilkDetail.MilkGiven; empToCustMilkVM.MilkType = existingCustomerMilkDetail.MilkType; empToCustMilkVM.EmployeeId = existingCustomerMilkDetail.EmployeeId; empToCustMilkVM.Id = existingCustomerMilkDetail.Id; } } return(View(empToCustMilkVM)); }
public ActionResult Create(EmpToCustMilkVM empToCustMilkVM) { if (ModelState.IsValid) { if (empToCustMilkVM.Id == 0) { db.StaffGivenMilkToCustomer.Add(new StaffGivenMilkToCustomer() { CustomerId = empToCustMilkVM.CustomerId, Date = empToCustMilkVM.Date, EmployeeId = empToCustMilkVM.EmployeeId, MilkGiven = empToCustMilkVM.MilkGiven, MilkType = empToCustMilkVM.MilkType }); } else { var existingCustomerMilkDetail = db.StaffGivenMilkToCustomer.FirstOrDefault(c => c.Id == empToCustMilkVM.Id); if (existingCustomerMilkDetail == null) { return(RedirectToAction("Index")); } else { existingCustomerMilkDetail.CustomerId = empToCustMilkVM.CustomerId; existingCustomerMilkDetail.Date = empToCustMilkVM.Date; existingCustomerMilkDetail.MilkGiven = empToCustMilkVM.MilkGiven; existingCustomerMilkDetail.MilkType = empToCustMilkVM.MilkType; existingCustomerMilkDetail.EmployeeId = empToCustMilkVM.EmployeeId; } } db.SaveChanges(); return(RedirectToAction("Index")); } return(View(empToCustMilkVM)); }