public ActionResult Edit(long id, XsQuestion questionToSave, FormCollection collection) { try { questionToSave = questionRepository.GetById(id); if (null != questionToSave) { questionToSave.RemoveAllTags(); questionToSave.Tags = new List<XsTag>(); String tags = RemoveExtraSpaces(collection["QuestionTags"].ToString()); String[] qTags = tags.Split(' '); foreach (String tag in qTags) { if (!String.IsNullOrEmpty(tag)) { String tagInvariantName = tag.ToLowerInvariant(); XsTag currentTag = tagRepository.GetTagByName(tagInvariantName); if (null == currentTag) { currentTag = new XsTag(); currentTag.Name = tagInvariantName; } questionToSave.AddTag(currentTag); } } questionToSave.Title = collection["Title"].ToString(); questionToSave.Content = collection["Content"].ToString(); questionToSave.ContentHtml = new Markdown().Transform(questionToSave.Content); questionToSave.Excerpt = Utils.CreateContentExcerpt(questionToSave.ContentHtml); questionToSave.UpdateDT = DateTime.UtcNow; ModelState.AddModelErrors(questionToSave.GetRuleViolations()); if (ModelState.IsValid) { try { questionToSave.ToAddToSearchIndex = true; questionRepository.Save(questionToSave); return RedirectToAction("Details", "Questions", new { id = questionToSave.ID, seoName = questionToSave.SlugTitle }); } catch (Exception ex) { log.Error("There was an error when saving the question in the DB " + ex.Message + "Inner Exception " + ex.InnerException); ViewData["ErrorMessage"] = "There was an error when saving the question in the DB!"; return View("Error"); } } else { return View(questionToSave); } } } catch { return View(questionToSave); } return View(questionToSave); }
public ActionResult Create(XsQuestion questionToCreate, String button, String QuestionTags) { questionToCreate.CreationDT = DateTime.UtcNow; questionToCreate.UpdateDT = DateTime.UtcNow; if (button.Equals("SaveDraft")) { questionToCreate.Status = XsStatus.Draft; } else { questionToCreate.Status = XsStatus.Published; questionToCreate.PublishedDT = DateTime.UtcNow; } String tags = RemoveExtraSpaces(QuestionTags); String[] qTags = tags.Split(' '); foreach (String tag in qTags) { if (!String.IsNullOrEmpty(tag)) { String tagInvariantName = tag.ToLowerInvariant(); XsTag currentTag = tagRepository.GetTagByName(tagInvariantName); if (null == currentTag) { currentTag = new XsTag(); currentTag.Name = tagInvariantName; } questionToCreate.AddTag(currentTag); } } questionToCreate.Content = questionToCreate.Content.Replace("<script", "[script").Replace("</script>", "[/script]"); questionToCreate.ContentHtml = new Markdown().Transform(questionToCreate.Content); questionToCreate.Excerpt = Utils.CreateContentExcerpt(questionToCreate.ContentHtml); ModelState.AddModelErrors(questionToCreate.GetRuleViolations()); if (ModelState.IsValid) { try { //TODO: to add other article attributes XsUser author = userRepository.GetByUsername(User.Identity.Name); if (null == author) { author = new XsUser(); author.Username = User.Identity.Name; author.QuestionCount = 1; } author.QuestionCount++; questionToCreate.Author = author; questionRepository.Save(questionToCreate); ViewData["QuestionTitle"] = questionToCreate.Title; return View("Confirm"); } catch (Exception ex) { log.Error("There was an error when saving the question in the DB " + ex.Message + "Inner Exception " + ex.InnerException ); ViewData["ErrorMessage"] = "There was an error when saving the question in the DB!"; return View("Error"); } } return View(); }