// GET: ToDoItems/Edit/5 public async Task <ActionResult> Edit(int id) { var item = await _context.ToDoItem.FirstOrDefaultAsync(ti => ti.Id == id); var loggedInUser = await GetCurrentUserAsync(); var ToDoStatuses = await _context.ToDoStatus .Select(td => new SelectListItem() { Text = td.Status, Value = td.Id.ToString() }) .ToListAsync(); var viewModel = new ToDoItemFormViewModel() { Id = id, Title = item.Title, ToDoStatusId = item.ToDoStatusId, ToDoStatusOptions = ToDoStatuses, }; if (item.ApplicationUserId != loggedInUser.Id) { return(NotFound()); } return(View(viewModel)); }
public async Task <ActionResult> Edit(int id, ToDoItemFormViewModel toDoViewItem) { try { var toDoItem = new ToDoItem() { Id = toDoViewItem.Id, Title = toDoViewItem.Title, ToDoStatusId = toDoViewItem.ToDoStatusId }; var user = await GetCurrentUserAsync(); toDoItem.ApplicationUserId = user.Id; _context.ToDoItem.Update(toDoItem); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
// GET: ToDoItems/Create public async Task <ActionResult> Create() { var ToDoStatuses = await _context.ToDoStatus .Select(td => new SelectListItem() { Text = td.Status, Value = td.Id.ToString() }) .ToListAsync(); var viewModel = new ToDoItemFormViewModel(); viewModel.ToDoStatusOptions = ToDoStatuses; return(View(viewModel)); }