public async Task <IActionResult> Details([Bind("CategoryID,AuthorID,ArticleTitle,ArticlePost,PublishDate")] CategoryBlogViewModel viewModel) { if (ModelState.IsValid) { Blog blog = new Blog(); blog.AuthorID = viewModel.AuthorID; blog.ArticleTitle = viewModel.ArticleTitle; blog.ArticlePost = viewModel.ArticlePost; blog.PublishDate = viewModel.PublishDate; Category category = await _context.Category .SingleOrDefaultAsync(m => m.ID == viewModel.CategoryID); if (category == null) { return(NotFound()); } blog.Category = category; _context.Blog.Add(blog); await _context.SaveChangesAsync(); viewModel = await GetCategoryBlogViewModelFromCategory(category); } return(View(viewModel)); }
private async Task <CategoryBlogViewModel> GetCategoryBlogViewModelFromCategory(Category category) { CategoryBlogViewModel viewModel = new CategoryBlogViewModel(); viewModel.Category = category; List <Blog> blogs = await _context.Blog .Where(m => m.Category == category).ToListAsync(); viewModel.Blogs = blogs; return(viewModel); }
public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } Category category = await _context.Category .SingleOrDefaultAsync(m => m.ID == id); if (category == null) { return(NotFound()); } CategoryBlogViewModel viewModel = await GetCategoryBlogViewModelFromCategory(category); //var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); will give the user's userId var userName = User.FindFirstValue(ClaimTypes.Name); ViewData["userid"] = userName; return(View(viewModel)); }