public ActionResult Edit(PostEditModelView postEditModel, int[] selectedTags) { if (selectedTags != null) { postEditModel.Post.Tags = Mapper.Map <IEnumerable <TagDTO>, List <TagModel> >(tagService.GetAll().Where(t => selectedTags.Contains(t.Id)).ToList()); ModelState["Post.Tags"].Errors.Clear(); } if (ModelState.IsValid) { try { var postDto = Mapper.Map <PostModel, PostDTO>(postEditModel.Post); if (User.IsInRole(Role.Admin) || User.IsInRole(Role.Moderator)) { postDto.IsPublished = true; } else { postDto.IsPublished = false; TempData["message"] = string.Format("Ваш пост \"{0}\" был отредактирован и отправлен модератору.", postEditModel.Post.Title); } postService.UpdatePost(postDto); return(RedirectToAction("List")); } catch (ValidationException ex) { ModelState.AddModelError(ex.Property, ex.Message); } } postEditModel.Categories = GetCategories(); postEditModel.Tags = GetTags(); return(View("Create", postEditModel)); }
public ActionResult EditModal(PostEditModelView postEditModel, int[] selectedTags) { if (selectedTags != null) { postEditModel.Post.Tags = Mapper.Map <IEnumerable <TagDTO>, List <TagModel> >(tagService.GetAll().Where(t => selectedTags.Contains(t.Id)).ToList()); ModelState["Post.Tags"].Errors.Clear(); } if (ModelState.IsValid) { try { var postDto = Mapper.Map <PostModel, PostDTO>(postEditModel.Post); if (User.IsInRole(Role.Admin) || User.IsInRole(Role.Moderator)) { postDto.IsPublished = true; } postService.UpdatePost(postDto); return(new HttpStatusCodeResult(HttpStatusCode.OK)); } catch (ValidationException ex) { ModelState.AddModelError(ex.Property, ex.Message); } } postEditModel.Categories = GetCategories(); postEditModel.Tags = GetTags(); ViewBag.DialogTitle = "Редактирование поста"; return(PartialView("CreateModal", postEditModel)); }
public ActionResult EditModal(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PostModel postViewModel = Mapper.Map <PostDTO, PostModel>(postService.GetPost(id)); if (postViewModel == null) { return(HttpNotFound()); } if (postViewModel.User.Login == User.Identity.Name || User.IsInRole(Role.Admin) || User.IsInRole(Role.Moderator)) { PostEditModelView post = new PostEditModelView() { Post = postViewModel, Categories = GetCategories(), Tags = GetTags(), }; ViewBag.DialogTitle = "Редактирование поста"; return(PartialView("CreateModal", post)); } else { return(null); } }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PostModel postViewModel = Mapper.Map <PostDTO, PostModel>(postService.GetPost(id)); if (postViewModel == null) { return(HttpNotFound()); } if (postViewModel.User.Login == User.Identity.Name || User.IsInRole(Role.Admin) || User.IsInRole(Role.Moderator)) { PostEditModelView post = new PostEditModelView() { Post = postViewModel, Categories = GetCategories(), Tags = GetTags(), }; return(View("Create", post)); } else { return(RedirectToAction("Show", id)); } }
public ActionResult Create() { PostEditModelView post = new PostEditModelView() { Post = new PostModel(), Categories = GetCategories(), Tags = GetTags(), }; return(View(post)); }
public ActionResult CreateModal() { PostEditModelView post = new PostEditModelView() { Post = new PostModel(), Categories = GetCategories(), Tags = GetTags(), }; ViewBag.DialogTitle = "Новый пост"; return(PartialView(post)); }
// Previous page public static MvcHtmlString PostTags(this HtmlHelper html, PostEditModelView post) { //var li = $("<li>").attr("id", "tag_id_" + tagId).attr("class", "search-choice collective"); //li.append($("<span>").attr("class", "text").append(tag)); //li.append($("<a>").attr("href", "#").attr("class", "search-choice-close").attr("rel", tagId).append("| x")); //li.append($("<input />", { type: 'checkbox', value: tagId, name: "selectedTags", checked: "checked"}).attr("style", "display: none")); StringBuilder result = new StringBuilder(); foreach (var postTag in post.Post.Tags) { // li TagBuilder tagLi = new TagBuilder("li"); tagLi.MergeAttribute("id", "tag_id_" + postTag.Id); tagLi.AddCssClass("search-choice collective"); // span TagBuilder tag = new TagBuilder("span"); tag.AddCssClass("text"); tag.InnerHtml = postTag.Name; tagLi.InnerHtml += tag.ToString(); // a tag = new TagBuilder("a"); tag.MergeAttribute("href", "#"); tag.MergeAttribute("rel", postTag.Id.ToString()); tag.AddCssClass("search-choice-close"); tag.InnerHtml = "| x"; tagLi.InnerHtml += tag.ToString(); // checkbox tag = new TagBuilder("input"); tag.MergeAttribute("type", "checkbox"); tag.MergeAttribute("value", postTag.Id.ToString()); tag.MergeAttribute("name", "selectedTags"); tag.MergeAttribute("checked", "checked"); tag.MergeAttribute("style", "display: none"); tagLi.InnerHtml += tag.ToString(); var tagRemove = post.Tags.FirstOrDefault(t => t.Value == postTag.Id.ToString()); post.Tags.Remove(tagRemove); result.Append(tagLi.ToString()); } return(MvcHtmlString.Create(result.ToString())); }