public async Task <ActionResult> Edit(int id, ToDoItemCreateViewModel toDoItemCreateViewModel) { try { var toDoItem = new ToDoItem() { Id = id, Title = toDoItemCreateViewModel.Title, TodoStatusId = toDoItemCreateViewModel.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/Edit/5 public async Task <ActionResult> Edit(int id) { var allToDoStatuses = await _context.ToDoStatus .Select(d => new SelectListItem() { Text = d.Title, Value = d.Id.ToString() }) .ToListAsync(); var toDoItem = _context.ToDoItem.FirstOrDefault(item => item.Id == id); var user = await GetCurrentUserAsync(); var viewModel = new ToDoItemCreateViewModel() { Title = toDoItem.Title, ApplicationUserId = user.Id, TodoStatusId = toDoItem.TodoStatusId, ToDoStatusOptions = allToDoStatuses }; if (toDoItem.ApplicationUserId != user.Id) { return(NotFound()); } return(View(viewModel)); }
// GET: ToDoItems/Create public async Task <ActionResult> Create() { var allToDoStatuses = await _context.ToDoStatus .Select(d => new SelectListItem() { Text = d.Title, Value = d.Id.ToString() }) .ToListAsync(); var viewModel = new ToDoItemCreateViewModel(); viewModel.ToDoStatusOptions = allToDoStatuses; return(View(viewModel)); }