Пример #1
0
        public async Task<IActionResult> Create(ToDoItemViewModel toDoItem)
        {
            if (ModelState.IsValid) // if the form is valid
            {
                try // try creating a new item
                {
                    using (var context = new ToDoListDBContext())
                    {

                        var newItem = new ToDoItem() // create new item from our ViewModel
                        { 
                            Finished = false,
                            Title = toDoItem.Title,
                            UserId = toDoItem.UserId 
                        };
                        //add new item to the database
                        context.ToDoItem.Add(newItem);
                        await context.SaveChangesAsync();
                    }
                }
                catch(Exception e) // if something went wrong 
                {
                    return RedirectToAction("error", "home", new { error = e.Message }); // return error page
                }
            }
            return RedirectToAction("list","account", new { userid = toDoItem.UserId });
        }