public async Task <ActionResult> OnPost() { if (!ModelState.IsValid) { Types = typeData.GetAll(); typeList = new SelectList(Types, "blogTypeId", "type"); return(Page()); } //Upload image or use default image string filename = null; if (Input.blogImageURL != null) { string uploadFolder = Path.Combine(hostEnvironment.WebRootPath, "images/BlogImages"); filename = Guid.NewGuid().ToString() + "_" + Input.blogImageURL.FileName; string filePath = Path.Combine(uploadFolder, filename); await Input.blogImageURL.CopyToAsync(new FileStream(filePath, FileMode.Create)); } else { filename = "new.jpg"; } //Create new Blog Blog = new Blog { blogTypeId = Input.blogTypeId, name = Input.name, body = Input.body, blogImageURL = filename, approved = false, userId = User.Claims.First(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value }; blogData.Add(Blog); if (await blogData.Commit()) { TempData["Message"] = $"Blog Added"; return(RedirectToPage("../Index", new { blogTypeId = Blog.blogTypeId })); } return(RedirectToPage("./NotFound")); }
public IActionResult OnPost() { if (!ModelState.IsValid) { return(Page()); } if (BlogDetails.Id > 0) { _blogData.Update(BlogDetails); TempData["Message"] = "Post Updated!"; } else { _blogData.Add(BlogDetails); TempData["Message"] = "Post Added!"; } _blogData.Commit(); //redirects to index return(RedirectToPage("./Index")); }