public async Task <IActionResult> CreateAsync(PostViewModel vm) { // if (HttpContext.Request.Form?.Files[0] != null) // { // var file = HttpContext.Request.Form.Files[0]; // vm.FormFile = file; // } // if (!TryValidateModel(vm)) // { // ModelState.AddModelError(string.Empty, "some validation error occured"); // return View(vm); // } if (!ModelState.IsValid) { return(View(vm)); } var post = vm.ToModel(); post.ImageName = await _fileService.SaveFileAsync(vm.FormFile, Post.PostBaseDirectory); await _context.Posts.AddAsync(post); await _context.SaveChangesAsync(); return(LocalRedirect("~/")); }
public IActionResult AddPost([FromForm] PostViewModel post) { post.Username = User.FindFirst(c => c.Type == ClaimTypes.NameIdentifier).Value; _postsBll.Add(post.ToModel()); return(RedirectToAction(Names.FeedControllerIndex)); }