public ActionResult Create(Content content) { if (ModelState.IsValid) { //Xử lý MetaTitle, sau đó insert content đã được xử lý vào database if (!string.IsNullOrEmpty(content.Name)) { content.MetaTitle = StringHelper.ToUnsignString(content.Name); } var dao = new ContentDao(); long id = dao.Insert(content); //Xử lý bảng Tag, ContentTag if (!string.IsNullOrEmpty(content.Tags)) { string[] tags = content.Tags.Split(','); foreach (var tag in tags) { var tagId = StringHelper.ToUnsignString(tag); var tagDao = new TagDao(); var contentTagDao = new ContentTagDao(); var existedTag = tagDao.CheckTag(tagId); //insert vào bảng Tag trong database if (!existedTag) { tagDao.Insert(tagId, tag); } //insert vào bảng ContentTag contentTagDao.Insert(content.ID, tagId); } } if (id > 0) { SetAlert("Create a new content successfully.", "success"); return(RedirectToAction("Index", "Content")); } else { ModelState.AddModelError("", "Create a new content failed."); return(RedirectToAction("Create", "Content")); } } SetContentCategoryIDViewBag(); return(View(content)); }
public ActionResult Edit(Content content) { if (ModelState.IsValid) { //Xử lý MetaTitle, sau đó update content đã được xử lý vào database if (!string.IsNullOrEmpty(content.Name)) { content.MetaTitle = StringHelper.ToUnsignString(content.Name); } var result = new ContentDao().Update(content); //Xử lý bảng Tag, ContentTag var contentTagDao = new ContentTagDao(); contentTagDao.RemoveAllContentTag(content.ID); if (!string.IsNullOrEmpty(content.Tags)) { string[] tags = content.Tags.Split(','); foreach (var tag in tags) { var tagId = StringHelper.ToUnsignString(tag); var tagDao = new TagDao(); var existedTag = tagDao.CheckTag(tagId); //insert vào bảng Tag trong database if (!existedTag) { tagDao.Insert(tagId, tag); } //insert vào bảng ContentTag contentTagDao.Insert(content.ID, tagId); } } if (result) { SetAlert("Edit this content successfully.", "success"); } else { ModelState.AddModelError("", "Edit this content failed."); } } SetContentCategoryIDViewBag(content.ContentCategoryID); return(View(content)); }