Пример #1
0
        public async Task <IActionResult> Create([FromRoute] int?id, string NewWriter, IFormFile Picture, [Bind("Language,Category,Writer,Title,Subtitle,Text")] Article article)
        {
            ModelState.Remove("Category.Name");
            ModelState.Remove("Writer.Name");

            if (ModelState.IsValid)
            {
                if (id == null)
                {
                    return(ValidationProblem(detail: "Issue Id cannot be null"));
                }

                article.Issue = await context.Issue.FindAsync(id);

                article.DateTime = DateTime.Now;

                // Assign value to the MemberID that refers to the writer of the article
                article.Category = context.Category
                                   .First(c => c.Name == article.Category.Name);

                articleService.SetArticleWriter(article,
                                                (article.Writer.Name == "+ NEW WRITER") ? NewWriter : article.Writer.Name);

                if (Picture != null)
                {
                    await articleService.AddArticlePicture(article, Picture, env.WebRootPath);
                }

                context.Add(article);
                await context.SaveChangesAsync();

                logger.Log($"{HttpContext.User.Identity.Name} created article of title `{article.Title}` and ID `{article.Id}`");

                return(RedirectToAction(nameof(Index), new { id = id }));
            }

            return(View(article));
        }