public ActionResult Author(AuthorViewModel authorViewModel) { bool updateSuccess = false; if (ModelState.IsValid) { try { AuthorServices.Upsert(authorViewModel); updateSuccess = true; } catch (DataAccessException e) { ViewBag.error = e.Message; return View(authorViewModel); } catch (DoesNotExistException e) { ViewBag.error = e.Message; return View(authorViewModel); } } if (updateSuccess) return RedirectToAction("Author", "Search", new { search = authorViewModel.FirstName + " " + authorViewModel.LastName }); else return View(authorViewModel); }
/// <summary> /// Insert into (if Aid is zero), or update an author in, the database. /// </summary> /// <param name="authorViewModel">The AuthorViewModel to upsert.</param> /// <exception cref="Services.Exceptions.DataAccessException"> /// Thrown when an error occurs in the data access layer.</exception> /// <exception cref="Services.Exceptions.DoesNotExistException"> /// Throw when an update fails.</exception> public static void Upsert(AuthorViewModel authorViewModel) { Author author = Mapper.Map<Author>(authorViewModel); if(author.Aid != 0) { if (!Author.Update(author)) { throw new DoesNotExistException("Författaren kunde inte uppdateras."); } } else { if (!Author.Insert(author)) { throw new DataAccessException("Oväntat fel när en författare skulle skapas."); } } }
public ActionResult Author(int? authorid) { AuthorViewModel author = new AuthorViewModel(); if (TempData["error"] != null) { ViewBag.error = TempData["error"].ToString(); TempData["error"] = null; } try { if (authorid != null) { author = AuthorServices.GetAuthor((int)authorid); } } catch (DoesNotExistException e) { ViewBag.error = e.Message; } catch (DataAccessException e) { ViewBag.error = e.Message; } return View(author); }