public async Task <IActionResult> Add(AuthorAddViewModel viewModel) { /* Inutile grazie al [Required]. */ //if (viewModel == null || string.IsNullOrWhiteSpace(viewModel.Name)) //{ // ModelState.AddModelError(nameof(viewModel.Name), "Nome vuoto!"); // return View(viewModel); //} await _context.Authors.AddAsync(new Author(0, viewModel.Name)); await _context.SaveChangesAsync(); return(View(viewModel)); }
/// <summary> /// Checking author's input and sending it to the database /// </summary> /// <returns>True if author sent to DB, otherwise false</returns> private bool AddItemAuthor() { AuthorAddViewModel viewmodel = (AuthorAddViewModel)DataContext; Author author = viewmodel.MyAuthor; if (string.IsNullOrEmpty(author.LastName)) { MessageBox.Show("Last name cannnot be empty."); return(false); } try { int id = MSAConnectionDB.SaveAuthorToDB(author); author.AuthorId = id; Authors.AddAuthor(author); ActivateMainWindow(); } catch (Exception e) { MessageBox.Show(e.Message); } return(true); }
/// <summary> /// Showing author's adding view /// </summary> /// <param name="sender">The source of the event</param> /// <param name="e">The instance containing the event data</param> private void AuthorAddView_Selected(object sender, RoutedEventArgs e) { DataContext = new AuthorAddViewModel(); }