public ActionResult AddBlog(AddBlogViewModel addBlogViewModel) { Response response = new Response() { Code = 0 }; Blog blog; if (ModelState.IsValid) { var category = categoryManager.Find(addBlogViewModel.CategoryId); if (category == null || category.Type != CategoryType.General) { response.Message = "栏目不匹配或者未找到!"; return(Json(response)); } if (blogManager.Find(b => b.Title == addBlogViewModel.Title) != null) { response.Message = "标题重复了!"; return(Json(response)); } blog = Mapper.Map <Blog>(addBlogViewModel); blog.Category = category; response = blogManager.AddBlog(blog); } else { response.Message = "输入不合法!"; } return(Json(response)); }
public ActionResult Add(AddBlogViewModel vm) { if (!ModelState.IsValid) { return(View(vm)); } if (vm.Image == null) { ModelState.AddModelError("", "Выберите картинку!"); return(View(vm)); } var result = SaveAs(vm.Image, PlatformConfiguration.UploadedBlogPath); AddBlogCommandResult commandResult = Command.For <AddBlogCommandResult>().Execute(new AddBlogCommand { Title = vm.Title, PreviewContent = vm.PreviewContent, HtmlContent = vm.HtmlContent, IsPublished = vm.IsPublished, FileId = result.File.Id }); return(RedirectToAction("Details", new { id = commandResult.Blog.Id })); }
public async Task <IActionResult> AddAsync(AddBlogViewModel model) { _context.Add(new Blog() { Name = model.Name, Url = model.Url }); await _context.SaveChangesAsync(); TempData["Messages"] = $"Blog '{model.Name}' added."; return(RedirectToAction(nameof(Index))); }
public ActionResult Edit(AddBlogViewModel model) { model.BlogPost.BlogPostHashTags = new List <HashTag>(); if (model.SelectedHashtagIds != null) { hashRepo.RemoveHashTagsFromBlog(model.BlogPost.BlogPostId); foreach (var item in model.SelectedHashtagIds) { if (hashRepo.GetHashTag(item).Success) { var response = hashRepo.GetHashTag(item); model.BlogPost.BlogPostHashTags.Add(response.HashTag); hashRepo.AddHashToPost(item, model.BlogPost.BlogPostId); } } BlogPost blog = blog = model.BlogPost; if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0) { var savepath = Server.MapPath("~/Images"); string fileName = Path.GetFileNameWithoutExtension(model.ImageUpload.FileName); string extension = Path.GetExtension(model.ImageUpload.FileName); var filePath = Path.Combine(savepath, fileName + extension); int counter = 1; while (System.IO.File.Exists(filePath)) { filePath = Path.Combine(savepath, fileName + counter.ToString() + extension); counter++; } model.ImageUpload.SaveAs(filePath); model.BlogPost.HeaderImage = Path.GetFileName(filePath); } else { blog.HeaderImage = repo.GetBlogById(model.BlogPost.BlogPostId).BlogPost.HeaderImage; } blog.CatagoryId = model.Catagory.CatagoryId; blog.BlogPostHashTags = model.BlogPost.BlogPostHashTags; blog.Approved = false; repo.UpdateBlogPost(blog); return(RedirectToAction("EditPost")); } return(RedirectToAction("EditPost")); }
public ActionResult AddPost(AddBlogViewModel model) { var hashMgr = HashTagManagerFactory.Create(); var cateMgr = CategoryRepoManagerFactory.Create(); var cResponse = cateMgr.GetCatagory(model.CatagoryId); model.Catagory = cResponse.CatagoryGot; model.BlogPost.CatagoryId = model.Catagory.CatagoryId; model.BlogPost.BlogPostHashTags = new List <HashTag>(); model.BlogPost.Approved = false; model.BlogPost.Id = User.Identity.GetUserId(); model.BlogPost.UserName = User.Identity.Name; foreach (var id in model.SelectedHashtagIds) { var response = hashMgr.GetHashTag(id); model.BlogPost.BlogPostHashTags.Add(response.HashTag); } if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0) { var savepath = Server.MapPath("~/Images"); string fileName = Path.GetFileNameWithoutExtension(model.ImageUpload.FileName); string extension = Path.GetExtension(model.ImageUpload.FileName); var filePath = Path.Combine(savepath, fileName + extension); int counter = 1; while (System.IO.File.Exists(filePath)) { filePath = Path.Combine(savepath, fileName + counter.ToString() + extension); counter++; } model.ImageUpload.SaveAs(filePath); model.BlogPost.HeaderImage = Path.GetFileName(filePath); } if (ModelState.IsValid) { var blogMgr = BlogPostRepoManagerFactory.Create(); blogMgr.AddBlog(model.BlogPost); return(RedirectToAction("EditPost", "Blogger", new { blogId = model.BlogPost.BlogPostId })); } return(View(model)); }
public ActionResult AddPost() { var model = new AddBlogViewModel(); var hashMgr = HashTagManagerFactory.Create(); var hashTags = hashMgr.GetAllHashTags(); var cataMgr = CategoryRepoManagerFactory.Create(); var categories = cataMgr.GetAllCategories(); model.HashTags = new SelectList(hashTags.HashTags, "HashTagId", "HashTagName"); model.Catagories = new SelectList(categories.Catagories, "CatagoryId", "CatagoryName"); return(View(model)); }