コード例 #1
0
 public async Task <IActionResult> CreateBlogPost(CreateBlogVm model)
 {
     if (!ModelState.IsValid)
     {
         model.Sports = Sport.All.GetOptionsWithoutAll();
         return(View(model));
     }
     if (model.Picture != null)
     {
         // путь к папке Files
         string path = "/images/Articles/" + model.Picture.FileName;
         // сохраняем файл в папку Files в каталоге wwwroot
         using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
         {
             await model.Picture.CopyToAsync(fileStream);
         }
         var article = new Articles
         {
             Description = model.Description,
             Content     = model.Content,
             Sport       = model.Sport,
             Header      = model.Header,
             Picture     = path,
             PublishedAt = DateTime.Now,
             UserId      = User.GetUserId(),
         };
         _db.Articles.Add(article);
         await _db.SaveChangesAsync();
     }
     return(RedirectToAction("BlogPosts"));
 }
コード例 #2
0
        public IActionResult CreateBlogPost()
        {
            var model = new CreateBlogVm()
            {
                Sports = Sport.All.GetOptionsWithoutAll(),
            };

            return(View(model));
        }
コード例 #3
0
        public IActionResult CreateBlog(CreateBlogVm vm)
        {
            try
            {
                var blog = new Blog
                {
                    Name = vm.Name
                };

                _blogContext.Blogs.Add(blog);

                _blogContext.SaveChanges();

                return(RedirectToAction(nameof(Blog), new { id = blog.BlogId }));
            }
            catch (Exception ex)
            {
                return(Ok(new
                {
                    ex.Message,
                    InnerMessage = ex.InnerException?.Message
                }));
            }
        }