public async Task <ActionResult> Create(VocabWordCreateViewModel vocabWordCreateViewModel) { try { var user = await GetCurrentUserAsync(); var vocabWord = new VocabWord { Word = vocabWordCreateViewModel.Word, Definition = vocabWordCreateViewModel.Definition, SoftwareLanguageId = vocabWordCreateViewModel.SoftwareLanguageId, }; //Set application user id to the the the lod in user's id vocabWord.ApplicationUserId = user.Id; //add the new todoitem to the list of todoitems? Or promise to add the the todoitems to the db. _context.VocabWords.Add(vocabWord); //Save changes to the databse await _context.SaveChangesAsync(); //Return to the list of todoitems once you've created a new todoItem return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public async Task <ActionResult> Edit(int?id, VocabWordCreateViewModel vocabWordCreateViewModel) { try { var allSoftwareLanguages = await _context.SoftwareLanguages .Select(l => new SelectListItem() { Text = l.Name, Value = l.Id.ToString() }) .ToListAsync(); var vocabWord = _context.VocabWords.FirstOrDefault(item => item.Id == id); //This is getting the current user who is logged in var user = await GetCurrentUserAsync(); //using the same view model as the create vocabWord.Word = vocabWordCreateViewModel.Word; vocabWord.Definition = vocabWordCreateViewModel.Definition; vocabWord.ApplicationUserId = user.Id; vocabWord.SoftwareLanguageId = vocabWordCreateViewModel.SoftwareLanguageId; _context.VocabWords.Update(vocabWord); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { return(View()); } }
// GET: VocabWords/Create public async Task <ActionResult> Create() { //Go through all of the to do statuses(select is like a map) and this creates a drop down of all of the statuses) var allSoftwareLanguages = await _context.SoftwareLanguages .Select(l => new SelectListItem() { Text = l.Name, Value = l.Id.ToString() }) .ToListAsync(); var viewModel = new VocabWordCreateViewModel(); viewModel.SoftwareLanguageOptions = allSoftwareLanguages; return(View(viewModel)); }
// GET: VocabWords/Edit/5 public async Task <ActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var allSoftwareLanguages = await _context.SoftwareLanguages .Select(l => new SelectListItem() { Text = l.Name, Value = l.Id.ToString() }) .ToListAsync(); var user = await GetCurrentUserAsync(); var vocabWord = await _context.VocabWords.FindAsync(id); //return the view model and populate it with the information from the returned todoItem var viewModel = new VocabWordCreateViewModel() { Id = vocabWord.Id, Word = vocabWord.Word, Definition = vocabWord.Definition, ApplicationUserId = user.Id, SoftwareLanguageId = vocabWord.SoftwareLanguageId, SoftwareLanguageOptions = allSoftwareLanguages }; if (vocabWord == null) { return(NotFound()); } //return the populated view Model return(View(viewModel)); }