public IActionResult Edit(EmployeeViewModel _employee) { //помимо использования атрибутов в модели, можно добавить проверку валидации в экшн-методе контроллера: //if (_employee.Age < 18 || _employee.Age > 99) ModelState.AddModelError("Age", "Некорректно указан возраст"); if (!ModelState.IsValid) { return(View(_employee)); //валидация } EmployeeViewModel employee = _employees.GetById(_employee.Id); if (employee == null) { _employees.AddNew(_employee); } else { employee.FirstName = _employee.FirstName; employee.LastName = _employee.LastName; employee.Patronymic = _employee.Patronymic; employee.Age = _employee.Age; employee.Position = _employee.Position; } _employees.Commit(); return(RedirectToAction("Employees")); }
public IActionResult BookEdit(BookViewModel _book) { BookViewModel book = _books.GetById(_book.Id); if (book == null) { _books.AddNew(_book); } else { book.Title = _book.Title; book.Author = _book.Author; book.PagesNumber = _book.PagesNumber; book.Year = _book.Year; } _books.Commit(); return(RedirectToAction("Books")); }