public async Task <IActionResult> Create(long id, [FromForm] Medication medication) { 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) { medication.MedicationId = default; medication.Infant = default; context.Medications.Add(medication); await context.SaveChangesAsync(); return(RedirectToAction("Index", "Dashboard", new { id = medication.InfantId })); } return(View("MedicationEditor", MedicationViewModelFactory.Create(medication, preSaveInfant))); }
// 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")); } Medication medication = new Medication { Date = DateTime.Now, InfantId = id }; return(View("MedicationEditor", MedicationViewModelFactory.Create(medication, infant))); }