public ActionResult CreateTopic(Topic topic, int sectId = 0, string message = "") { if (TopicIsValid(ref topic, sectId, message)) { int topicId = topics.AddTopic(new Topic() { dateTime = DateTime.Now, SectionID = sectId, Theme = topic.Theme }); messages.AddMessage(new Message() { message = message, dateTime = DateTime.Now, UserId = int.Parse(Session["id"].ToString()), TopicId = topicId, IsFirstMessage = true }); return RedirectToAction("ShowListOfTopic"); } else { topic.SectionID = sectId; ViewBag.Sections = sections.Sections; ViewBag.Message = message; return View(topic); } }
// Checking topic. // private bool TopicIsValid(ref Topic topic, int sectId, string message) { if (topic.Theme == null) topic.Theme = ""; if (topic.Theme.Length == 0) ModelState.AddModelError("Theme", "Название темы пустое"); if (topic.Theme.Length > 40) ModelState.AddModelError("Theme", "Название темы слишком длинное"); try { sections.Sections.Where(s => s.Id == sectId).First(); } catch { ModelState.AddModelError("sectId", "Раздела не существует"); } MessageIsValid(message); return ModelState.IsValid; }
public ActionResult EditTopic(Topic topic, int sectId, string message) { if (TopicIsValid(ref topic, sectId, message)) { topic.SectionID = sectId; topics.EditTopic(topic); messages.ChangeMessage(messages.Messages.Where(m => (m.TopicId == topic.Id) && (m.IsFirstMessage == true)).FirstOrDefault().Id, message); return RedirectToAction("ShowListOfTopic"); } else { topic.SectionID = sectId; ViewBag.Sections = sections.Sections; ViewBag.Message = message; return View(topic); } }