/// <summary> /// Toes the article edit model. /// </summary> /// <param name="article">The article.</param> /// <returns>Article View Model</returns> public static ArticleEditModel ToArticleEditModel(Article article) { ArticleEditModel model = new ArticleEditModel { ArticleId = article.ArticleId, Username = article.Username, Title = article.Title, Content = article.Content }; return model; }
/// <summary> /// Edits the specified article id. /// </summary> /// <param name="articleId">The article id.</param> /// <returns>Page Content</returns> public ViewResult Edit(int articleId) { Article article = repository.Articles.FirstOrDefault(b => b.ArticleId == articleId); ArticleEditModel model = new ArticleEditModel { ArticleId = article.ArticleId, Title = article.Title, Content = article.Content, Username = User.Identity.Name }; return View(model); }
/// <summary> /// Froms the article edit model. /// </summary> /// <param name="model">The model.</param> /// <returns>New Article</returns> public static Article FromArticleEditModel(ArticleEditModel model) { Article article = new Article { ArticleId = model.ArticleId, Modified = DateTime.Now, Title = model.Title, Username = model.Username, Content = model.Content }; return article; }
public ActionResult Edit(ArticleEditModel model) { if (ModelState.IsValid) { Article article = new Article(); if (repository.Articles.Count(b => b.ArticleId == model.ArticleId) > 0) { article = repository.Articles.FirstOrDefault(b => b.ArticleId == model.ArticleId); } else { article = ArticleMapping.FromArticleEditModel(model); } repository.SaveArticle(article); TempData["message"] = string.Format("{0} has been saved", model.Title); return RedirectToAction("Index"); } else { if (model.Content == null || model.Content.Length <= 0) { ModelState.AddModelError("", "Please enter some content."); } if (model.Title == null || model.Title.Length <= 0) { ModelState.AddModelError("", "Please enter a Title."); } else if(model.Title == null || model.Title.Length > 255) { ModelState.AddModelError("", "Title is too long, maximum length is 255 characters."); } return View(model); } }
public ActionResult EditModal(ArticleEditModel article) { Article newArticle = repository.Articles.FirstOrDefault(b => b.ArticleId == article.ArticleId); article.Username = User.Identity.Name; newArticle = ArticleMapping.FromArticleEditModel(article); if (ModelState.IsValid) { repository.SaveArticle(newArticle); return RedirectToAction("Index"); } else { // there is something wrong with the data values return View(article); } }
/// <summary> /// Edits the modal. /// </summary> /// <param name="articleId">The article id.</param> /// <returns>Page Content</returns> public ViewResult EditModal(int articleId) { Article article = repository.Articles.FirstOrDefault(b => b.ArticleId == articleId); if (article != null) { ArticleEditModel model = new ArticleEditModel { ArticleId = article.ArticleId, Title = article.Title, Content = article.Content }; return View(model); } return View(); }