示例#1
0
        public async Task <IActionResult> CreatePost(PostDetailViewModel model)
        {
            if (model == null)
            {
                ShowAlertDanger("Could not create empty post.");
                return(RedirectToAction(nameof(Index)));
            }

            if (ModelState.IsValid)
            {
                var post = await _newsService.CreatePostAsync(model.Post, model.Publish);

                if (model.Publish)
                {
                    return(await SendEmailJobAsync(GetJobParameters(post)));
                }
                else
                {
                    ShowAlertSuccess($"Added Post \"{post.Title}\"!");
                    return(RedirectToAction(nameof(Index)));
                }
            }

            model.Action = nameof(CreatePost);
            model.Categories
                = new SelectList(await _newsService.GetAllCategoriesAsync(), "Id", "Name");

            return(View("PostDetail", model));
        }
        public async Task <IActionResult> CreatePost(PostDetailViewModel model)
        {
            if (ModelState.IsValid)
            {
                var postUrl = Url.Action(nameof(HomeController.Index),
                                         "Home",
                                         new { category = model.Post.CategoryId },
                                         HttpContext.Request.Scheme);

                var post = await _newsService.CreatePostAsync(model.Post, postUrl, model.Publish);

                ShowAlertSuccess($"Added Post \"{post.Title}\"!");
                return(RedirectToAction(nameof(Index)));
            }

            model.Action = nameof(CreatePost);
            model.Categories
                = new SelectList(await _newsService.GetAllCategoriesAsync(), "Id", "Name");

            return(View("PostDetail", model));
        }