// HTTP Get public IActionResult Create(long id) { if (!IsLoggedIn()) { return(RedirectToPage("/Account/Login")); } Infant infant = context.Infants.FirstOrDefault(i => i.InfantId == id); if (!IsInfantOwner(infant)) { return(RedirectToPage("/Error/Error404")); } Feeding feeding = new Feeding { StartTime = DateTime.Now, InfantId = id }; return(View("FeedingEditor", FeedingViewModelFactory.Create(feeding, infant))); }
public async Task <IActionResult> Create(long id, [FromForm] Feeding feeding) { if (!IsLoggedIn()) { return(RedirectToPage("/Account/Login")); } Infant preSaveInfant = context.Infants.AsNoTracking().FirstOrDefault(i => i.InfantId == id); if (!IsInfantOwner(preSaveInfant)) { return(RedirectToPage("/Error/Error404")); } if (ModelState.IsValid) { feeding.FeedingId = default; feeding.Infant = default; context.Feedings.Add(feeding); await context.SaveChangesAsync(); return(RedirectToAction("Index", "Dashboard", new { id = feeding.InfantId })); } return(View("FeedingEditor", FeedingViewModelFactory.Create(feeding, preSaveInfant))); }