public IActionResult Create() { BlogEditViewModel data = new BlogEditViewModel(); data.Categories = categoryRepository.GetAll(); return(View("BlogCreate", data)); }
private string ProcessUploadedFile(BlogEditViewModel model, Blog blog) { string uniqueFileName = null; if (model.Images != null && model.Images.Count > 0) { foreach (IFormFile photo in model.Images) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "userdata/blogs"); uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { photo.CopyTo(fileStream); } Image image = new Image(); image.ImageId = Guid.NewGuid(); image.ImagePath = uniqueFileName; BlogImages newImage = new BlogImages(); newImage.Blog = blog; newImage.Image = image; //context.BlogImages.Add(newImage); context.SaveChanges(); } } return(uniqueFileName); }
// GET: Blog/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Blog blog = db.Blog.Find(id); if (blog == null) { return(HttpNotFound()); } BlogEditViewModel model = new BlogEditViewModel() { Title = blog.Title, Description = blog.Description, IsPublished = blog.IsPublished, Content = blog.Content, Image = blog.Image, ImagePreview = blog.ImagePreview, Layout = blog.Layout, Notes = blog.Notes, }; return(View(model)); }
public async Task <IActionResult> Edit(int id, BlogEditViewModel viewModel) { if (id != viewModel.BlogId) { return(NotFound()); } if (ModelState.IsValid) { var blog = await _blogService.GetBlogById(id); if (blog == null) { return(NotFound()); } string imagePath = null; if (viewModel.ImageFile?.FileName != null) { imagePath = await _imageService.SaveBlogImage(viewModel.ImageFile, blog.ImagePath); } await _blogService.UpdateBlog(blog.BlogId, viewModel.BlogName, viewModel.Description, viewModel.BlogStyleId, imagePath); return(RedirectToAction(nameof(Index))); } return(View(viewModel)); }
public async Task <IActionResult> Edit(int id, BlogEditViewModel viewModel) { /* * We make sure that the id passed in the url is the same as the post id in the view. */ if (id != viewModel.Post.Id) { return(NotFound()); } /* * Update the post record on the database */ _DB.Posts.Update(viewModel.Post); /* * Then we save the changes to the database */ await _DB.SaveChangesAsync(); /* * Return to the index function */ return(RedirectToAction(nameof(Index))); }
public async Task EditChanges(BlogEditViewModel blogEditViewModel) { var blog = this.db.Blogs.FirstOrDefault(x => x.Id == blogEditViewModel.Id); blog.Description = blogEditViewModel.Description; blog.Title = blogEditViewModel.Title; blog.ImageUrl = blogEditViewModel.ImageUrl; await this.db.SaveChangesAsync(); }
public async Task <IActionResult> Edit(BlogEditViewModel blogEditViewModel) { if (!this.ModelState.IsValid) { return(this.View(blogEditViewModel)); } await this.blogService.EditChanges(blogEditViewModel); return(this.Redirect($"/Blogs/Details?id={blogEditViewModel.Id}")); }
public async Task <OkObjectResult> Get(BlogGetViewModel viewModel) { var entityIds = new List <long>(); if (viewModel.Id != default) { entityIds.Add(viewModel.Id); } var filterServiceModel = AutoMapper.Mapper.Map <BlogFilterServiceModel>(viewModel); filterServiceModel.Ids = entityIds; var blogs = _blogService.GetBlogs(filterServiceModel); if (string.IsNullOrEmpty(viewModel.SortField) == false) { if (viewModel.SortField == "title") { blogs = (viewModel.SortOrder == "ascend") ? blogs.OrderBy(o => o.Details.FirstOrDefault().Title) : blogs.OrderByDescending(o => o.Details.FirstOrDefault().Title); } } object actionOkResult = null; if (viewModel.GetMode == (int)GetMode.Paginated) { var pageList = await PaginatedList <BlogEntity> .CreateAsync(blogs, viewModel.Page, viewModel.PageSize); var result = new PageEntityViewModel <BlogEntity, BlogViewModel>(pageList, entity => BlogViewModel.FromEntity(entity)); actionOkResult = result; } else if (viewModel.GetMode == (int)GetMode.List) { var result = blogs.Select(o => BlogEditViewModel.FromEntity(o)); actionOkResult = result; } else { var blog = blogs.FirstOrDefault(o => o.Id == viewModel.Id || o.Name == viewModel.Name); var result = new BlogEditViewModel(); if (blog != null) { result = BlogEditViewModel.FromEntity(blog); } actionOkResult = result; } return(Ok(actionOkResult)); }
public ActionResult Edit(int id, BlogEditViewModel viewModel) { return(ViewOr404(viewModel, () => { if (SaveObject(viewModel.ModelObject, true)) { return RedirectToAction("Index"); } return View(viewModel); })); }
public ActionResult Edit(BlogEditViewModel model) { if (ModelState.IsValid) { var blog = _db.Blogs.Where(b => b.id == model.id).FirstOrDefault(); string postType = ""; /*if (model.Type == PostType.WeatherBlog) * { * postType = "Weather Blog"; * } * else * { * postType = "Weather News"; * * }*/ if (model.Picture == null) { blog.description = model.Description; blog.title = model.Title; blog.type = model.Type; _db.SaveChanges(); } else { string fileName = Path.GetFileNameWithoutExtension(model.Picture.FileName); string fileExtension = Path.GetExtension(model.Picture.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + fileExtension; string filePathString = "/Images/Blog_Pictures/" + fileName; fileName = Path.Combine(Server.MapPath("~/Images/Blog_Pictures/"), fileName); model.Picture.SaveAs(fileName); blog.imagePath = filePathString; blog.description = model.Description; blog.title = model.Title; blog.type = postType; _db.SaveChanges(); } TempData["Success"] = "Successfully Added"; ModelState.Clear(); model = new BlogEditViewModel(); return(View(model)); } return(View(model)); }
public ActionResult Edit(int id) { var model = _db.Blogs.Where(b => b.id == id).FirstOrDefault(); var viewModel = new BlogEditViewModel { id = model.id, Title = model.title, Description = model.description, Type = model.type }; return(View(viewModel)); }
public async Task <ActionResult> OnGetAsync() { if (!await _authorization.IsGrantedAsync(BloggingPermissions.Blogs.Update)) { return(Redirect("/")); } var blog = await _blogAppService.GetAsync(BlogId); Blog = ObjectMapper.Map <BlogDto, BlogEditViewModel>(blog); return(Page()); }
public IActionResult Create(BlogEditViewModel data, string[] categoryList) { if (ModelState.IsValid) { Blog blog = new Blog(); blog.BlogId = Guid.NewGuid(); blog.Title = data.Blog.Title; blog.Content = data.Blog.Content; blog.CreatedAt = DateTime.Now; blog.UpdatedAt = DateTime.Now; blogRepository.Insert(blog); blogRepository.Save(); using (var a = new AppDbContext(contextOptions)) { for (int i = 0; i < categoryList.Length; i++) { var category = a.Categories .Single(p => p.Id == Guid.Parse(categoryList[i])); a.Blogs.Include(p => p.BlogCategories).Single(p => p.BlogId == blog.BlogId).BlogCategories.Add(new BlogCategories { Blog = blog, Category = category }); a.SaveChanges(); } } if (data.Images.Any()) { string uniqueFileName = ProcessUploadedFile(data, blog); blog.PhotoPath = uniqueFileName; } else { blog.PhotoPath = null; } blogRepository.Update(blog); blogRepository.Save(); TempData["Success"] = "Operation Successful!"; return(RedirectToAction("index")); } else { return(View("BlogCreate", data)); } }
public ActionResult Edit(int id) { var blog = db.trnblogs.Single(a => a.ID == id); var viewModel = new BlogEditViewModel { ID = blog.ID, Title = blog.Title, Body = blog.Body, CategoryID = blog.CategoryID, ThumbnailImagePath = string.IsNullOrEmpty(blog.ThumbnailImagePath) ? "" : Path.Combine(Settings.Default.ImageUploadPath, blog.ThumbnailImagePath), Categories = GetList(ListType.Category), Active = blog.Active }; return(View(viewModel)); }
public async Task <IActionResult> Edit(BlogEditViewModel model) { var blogDb = _blogService.Get <BlogEditViewModel>(model.Id); if (blogDb.OwnerId != HttpContext.User.GetUserId()) { return(RedirectToAction(nameof(Index), "blog", new { @area = GlobalConstants.AdminArea })); } if (ModelState.IsValid) { var success = await _blogService.Edit(model); if (success) { return(RedirectToAction(nameof(Index), "blog", new { @area = GlobalConstants.AdminArea })); } } return(View(model)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var blog = await _blogService.GetBlogById((int)id); if (blog == null) { return(NotFound()); } var isOwner = await _authorizationService.AuthorizeAsync(User, blog, "OwnerPolicy"); if (!isOwner.Succeeded) { return(Forbid()); } var styles = await _blogService.GetAllBlogStyles(); var selectItems = styles.Select(m => new SelectListItem { Text = m.BlogStyleName, Value = m.BlogStyleId.ToString() }).ToList(); var viewModel = new BlogEditViewModel() { BlogId = (int)id, BlogName = blog.BlogName, Description = blog.Description, ImagePath = blog.ImagePath, Moderators = await _blogService.GetAllBlogModerators(blog.BlogId), Styles = selectItems, CurrentStyle = blog.BlogStyle, BlogStyleId = blog.BlogStyle.BlogStyleId }; return(View(viewModel)); }
// GET: Blog/Edit/5 public async Task <IActionResult> Edit(int?id) { /* * We double check an id has been passed * Otherwise we return not found. */ if (id == null) { return(NotFound()); } /* * We setup the blog edit view model. * Then we get the post from the database by id */ BlogEditViewModel viewModel = new BlogEditViewModel() { Post = await _DB.Posts.FindAsync(id), /* * Set up the categories from the database to a select list item. */ Categories = (IEnumerable <SelectListItem>)_DB.Categories.Select(m => new SelectListItem { Value = m.Id.ToString(), Text = m.Name, Selected = m.Id == 1 }) }; /* * If the post is null then we again return not found. */ if (viewModel.Post == null) { return(NotFound()); } return(View(viewModel)); }
public ActionResult Edit(HttpPostedFileBase file, BlogEditViewModel viewModel) { var blog = db.trnblogs.Single(a => a.ID == viewModel.ID); if (!ModelState.IsValid) { viewModel.ThumbnailImagePath = string.IsNullOrEmpty(blog.ThumbnailImagePath) ? "" : Path.Combine(Settings.Default.ImageUploadPath, blog.ThumbnailImagePath); viewModel.Categories = GetList(ListType.Category); return(View(viewModel)); } blog.CategoryID = viewModel.CategoryID; blog.Title = viewModel.Title; blog.Body = viewModel.Body; blog.UpdateDT = DateTime.Now; blog.Active = viewModel.Active; if (file != null) { var fileName = FileUploadManager.UploadAndSave(file); blog.ThumbnailImagePath = fileName; } db.SaveChanges(); var emailBody = string.Format(@"Hi EZ Management, <br /><br /> Blog <b>{0}</b> has been updated. <br /><br /> http://www.ezgoholiday.com/EN/Blog/Details/{1}", viewModel.Title, blog.ID); Util.SendEmail(viewModel.Title, emailBody, Properties.Settings.Default.EmailFrom, "", ""); return(RedirectToAction("Index")); }
private string ProcessUploadedFile(BlogEditViewModel model) { string uniqueFileName = null; if (model.Images != null && model.Images.Count > 0) { foreach (IFormFile photo in model.Images) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "userdata/blogs"); uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { photo.CopyTo(fileStream); } var currentBlog = context.Blogs.AsNoTracking().Include(p => p.BlogCategories).Single(p => p.BlogId == model.Blog.BlogId); Image image = new Image(); image.ImageId = Guid.NewGuid(); image.ImagePath = uniqueFileName; BlogImages newImage = new BlogImages(); newImage.Blog = currentBlog; newImage.Image = image; using (var a = new AppDbContext(contextOptions)) { a.Blogs.Include(p => p.BlogImages).Single(p => p.BlogId == model.Blog.BlogId).BlogImages.Add(newImage); a.SaveChanges(); } } } return(uniqueFileName); }
public IActionResult Edit(Guid id) { BlogEditViewModel data = new BlogEditViewModel(); data.Categories = categoryRepository.GetAll(); var categoriesImport = context.Blogs .Include(x => x.BlogCategories).ThenInclude(x => x.Category); var selectedCategories = categoriesImport.Where(x => x.BlogId == id).Select(x => x.BlogCategories).First().ToList(); for (int i = 0; i < selectedCategories.Count(); i++) { foreach (var category in data.Categories) { if (category.Id == selectedCategories[i].CategoryId) { category.IsChecked = true; } } } var images = context.Blogs.Include(p => p.BlogImages).ThenInclude(p => p.Image).ToList(); List <string> blogImages = new List <string>(); foreach (Blog p in images) { var filter = p.BlogImages; foreach (var pi in filter) { if (pi.BlogId == id) { blogImages.Add(pi.Image.ImagePath); } } } data.ImagePath = blogImages; //var allProjects = context.Projects.Include(p => p.ProjectCategories).ToList(); // List<Project> categoryRelatedProjects = new List<Project>(); // foreach (Project p in allProjects) // { // var filter = p.ProjectCategories; // foreach (var pr in filter) // { // if (pr.CategoryId.ToString() == "83278a7e-2a50-4914-62c9-08d79ca3d529") // { // categoryRelatedProjects.Add(p); // } // } // } data.Blog = blogRepository.GetById(id); return(View("BlogEdit", data)); }
public ActionResult Edit([Bind(Include = "ID,Title,Description,Content,Image,ImageRemove, ImageFile, ImagePreview, ImagePreviewRemove, ImagePreviewFile,Layout,IsPublished,Notes")] BlogEditViewModel model) { if (ModelState.IsValid) { Blog blog = db.Blog.Find(model.ID); string imagePath = blog.Image; // Checking if the image must be removed if (model.ImageRemove) { string imageFullPath = Request.MapPath(blog.Image); if (System.IO.File.Exists(imageFullPath)) { System.IO.File.Delete(imageFullPath); } imagePath = ""; } // Checking if there is any file if (model.ImageFile != null) { // Generating the full paths imagePath = System.IO.Path.Combine(UploadDirectory.path, model.ImageFile.FileName); // Uploading the files model.ImageFile.SaveAs(Server.MapPath(imagePath)); } string imagePreviewPath = blog.ImagePreview; // Checking if the image must be removed if (model.ImagePreviewRemove) { string imagePreviewFullPath = Request.MapPath(blog.ImagePreview); if (System.IO.File.Exists(imagePreviewFullPath)) { System.IO.File.Delete(imagePreviewFullPath); } imagePreviewPath = ""; } // Checking if there is any file if (model.ImagePreviewFile != null) { // Generating the full paths imagePreviewPath = System.IO.Path.Combine(UploadDirectory.path, model.ImagePreviewFile.FileName); // Uploading the files model.ImagePreviewFile.SaveAs(Server.MapPath(imagePreviewPath)); } blog.Title = model.Title; blog.Description = model.Description; blog.IsPublished = model.IsPublished; blog.Content = model.Content; blog.Image = imagePath; blog.ImagePreview = imagePreviewPath; blog.Layout = model.Layout; blog.Notes = model.Notes; blog.ModifDate = DateTime.Now; blog.ModifUserName = User.Identity.Name; db.Entry(blog).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("List")); } return(View(model)); }
// GET: Blog/Edit/5 public ActionResult Edit(int id) { var viewModel = new BlogEditViewModel(id); return(ViewOr404(viewModel, () => View(viewModel))); }
public IActionResult Update(BlogEditViewModel data, Guid[] categoryList) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadedFile(data); Blog blog = new Blog(); blog.BlogId = data.Blog.BlogId; blog.Title = data.Blog.Title; blog.Content = data.Blog.Content; blog.UpdatedAt = DateTime.Now; if (uniqueFileName != null) { blog.PhotoPath = uniqueFileName; } else { blog.PhotoPath = context.Blogs.AsNoTracking().Where(p => p.BlogId == data.Blog.BlogId).First().PhotoPath; } blogRepository.Update(blog); blogRepository.Save(); using (var a = new AppDbContext(contextOptions)) { var currentBlog = a.Blogs.Include(p => p.BlogCategories).Single(p => p.BlogId == data.Blog.BlogId); //foreach (var category in currentProject.ProjectCategories) //{ // currentProject.ProjectCategories.Remove(category); //} //context.SaveChanges(); //Delete Categories foreach (var item in currentBlog.BlogCategories.ToList()) { currentBlog.BlogCategories.Remove(item); a.SaveChanges(); } for (int i = 0; i < categoryList.Length; i++) { var category = a.Categories .Single(p => p.Id == categoryList[i]); //bool exists = context.Entry(category) // .Collection(m => m.ProjectCategories) // .Query() // .Any(x => x.CategoryId == categoryList[i]); //if (exists) // continue; // Add Categories currentBlog.BlogCategories.Add(new BlogCategories { Blog = currentBlog, Category = category }); a.SaveChanges(); } } TempData["Success"] = "Operation Successful!"; return(RedirectToAction("index")); } else { return(View("ProjectEdit", data)); } }
public async Task OnGet() { var blog = await _blogAppService.GetAsync(BlogId); Blog = ObjectMapper.Map <BlogDto, BlogEditViewModel>(blog); }