public IActionResult Add(AddMedViewModel addMedViewModel) { string user = User.Identity.Name; ApplicationUser userLoggedIn = _context.Users.Single(c => c.UserName == user); if (ModelState.IsValid) { Medication newMed = new Medication { Name = addMedViewModel.Name, Dosage = addMedViewModel.Dosage, TimesXDay = addMedViewModel.TimesXDay, Notes = addMedViewModel.Notes, RefillRate = addMedViewModel.RefillRate, UserID = userLoggedIn.Id }; // add to db and user list _context.Medication.Add(newMed); userLoggedIn.AllMeds.Add(newMed); // save changes _context.SaveChanges(); return(Redirect("/Medications/Index")); } return(View(addMedViewModel)); }
// GET: Medications/Add // To add a new medication to your list public IActionResult Add() { // IEnumerable<ToD> times = (ToD[])Enum.GetValues(typeof(ToD)); AddMedViewModel viewModel = new AddMedViewModel(); return(View(viewModel)); }
// To Add a new medication to your list. public IActionResult Add() { // query for the list of ToD's and pass into the view model IEnumerable <ToD> times = (ToD[])Enum.GetValues(typeof(ToD)); AddMedViewModel addMedViewModel = new AddMedViewModel(times); return(View(addMedViewModel)); }
public IActionResult Add(AddMedViewModel addMedViewModel) { string user = User.Identity.Name; ApplicationUser userLoggedIn = context.Users.Single(c => c.UserName == user); // Looked for this code for a long time, and thought I didn't use it, I wanted it written down... // var time = (ToD)System.Enum.Parse(typeof(ToD), addMedViewModel.SelectedTime); if (ModelState.IsValid) { Medication newMed = new Medication { Name = addMedViewModel.Name, Dosage = addMedViewModel.Dosage, //TimesXDay = addMedViewModel.TimesXDay, Notes = addMedViewModel.Notes, TimeOfDay = addMedViewModel.SelectedTime, Description = addMedViewModel.Description, RefillRate = addMedViewModel.RefillRate, PrescribingDoctor = addMedViewModel.PrescribingDoctor, ScripNumber = addMedViewModel.ScripNumber, Pharmacy = addMedViewModel.Pharmacy, PillsPerDose = addMedViewModel.PillsPerDose, UserID = userLoggedIn.Id }; // add to db and user list context.Medication.Add(newMed); userLoggedIn.AllMeds.Add(newMed); // save changes context.SaveChanges(); return(Redirect("/Medication/Index")); } return(View(addMedViewModel)); }