Esempio n. 1
0
        public async Task <IActionResult> Detail(int pid)
        {
            try
            {
                CategoryInfoAsync();
                GetInfo getInfo = new GetInfo(_db, _configuration, _cache);
                #region correlation posts 5
                var correlationPostResult = await getInfo.GetCorrelationPostInfoAsync(pid);

                var correlationPostMaps = _mapper.Map <List <Post>, List <PostDto> >(correlationPostResult);
                ViewBag.CorrelationPosts = new List <PostDto>();
                if (correlationPostMaps.Count > 0)
                {
                    ViewBag.CorrelationPosts = correlationPostMaps;
                }

                #endregion
                var queryPost = await _db.Post.Where(u => u.Id == pid).Include(u => u.PostTag).SingleOrDefaultAsync();

                queryPost.Views += 1;
                if (queryPost == null)
                {
                    TempData["Error"] = "文章不存在.";
                    return(Redirect("/home/error"));
                }
                var postMaps  = _mapper.Map <Post, PostDetailDto>(queryPost);
                var queryTags = from a in queryPost.PostTag
                                join b in _db.Tag.AsNoTracking()
                                on a.TagId equals b.Id
                                where a.PostId == pid && b.IsActive == true
                                orderby b.SortNo ascending
                                select b;
                var tagMaps = _mapper.Map <List <Tag>, List <TagDto> >(queryTags.ToList());
                postMaps.Tags = tagMaps;
                var output = postMaps;
                _db.Post.Attach(queryPost);
                _db.Entry(queryPost).State = EntityState.Modified;
                await _db.SaveChangesAsync();

                return(View(output));
            }
            catch (Exception ex)
            {
                TempData["Error"] = ex.Message;
                return(Redirect("/home/error"));
            }
            finally
            {
                _db.Dispose();
            }
        }