コード例 #1
0
        // GET: Admin/PostCategories/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            var vm = new PostCategoryIM
            {
                Active     = true,
                Importance = 0
            };

            if (id == null)
            {
                return(View(vm));
            }

            var category = await _context.PostCategories.FindAsync(id);

            if (category == null)
            {
                return(NotFound());
            }
            var model = _mapper.Map <PostCategoryIM>(category);

            var pm = await _context.PageMetas.FirstOrDefaultAsync(d => d.ModuleType == (short)ModuleType.ARTICLECATEGORY && d.ObjectId == category.Alias);

            if (pm == null)
            {
                return(View(model));
            }

            model.SEOTitle       = pm.Title;
            model.SEOKeywords    = pm.Keywords;
            model.SEODescription = pm.Description;

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Description,Importance,Active")] PostCategoryIM im)
        {
            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return(Json(AR));
            }
            if (id != im.Id)
            {
                AR.Setfailure("未发现此分类");
                return(Json(AR));
            }


            try
            {
                var model = await _context.PostCategories.FindAsync(id);

                model             = _mapper.Map(im, model);
                model.UpdatedBy   = User.Identity.Name;
                model.UpdatedDate = DateTime.Now;

                _context.Update(model);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostCategoryExists(im.Id))
                {
                    AR.Setfailure("未发现此分类");
                    return(Json(AR));
                }
                else
                {
                    AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.PostCategory));
                    return(Json(AR));
                }
            }
            //  return RedirectToAction(nameof(Index));

            AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.PostCategory));
            return(Json(AR));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("Id,Title,Description,Importance,Active")] PostCategoryIM im)
        {
            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return(Json(AR));
            }


            var model = _mapper.Map <PostCategory>(im);

            model.CreatedDate = DateTime.Now;
            model.CreatedBy   = User.Identity.Name;

            _context.Add(model);
            await _context.SaveChangesAsync();


            AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.PostCategory));
            return(Json(AR));
        }
コード例 #4
0
        public async Task <IActionResult> Edit([Bind("Id,Title,ImageUrl,Alias,Description,Importance,Active,SEOTitle,SEOKeywords,SEODescription")] PostCategoryIM im, int id = 0)
        {
            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return(Json(AR));
            }

            if (id == 0)
            {
                var model = _mapper.Map <PostCategory>(im);
                if (User.Identity != null)
                {
                    model.CreatedBy = User.Identity.Name;
                }
                model.CreatedDate = DateTime.Now;
                _context.Add(model);

                await _context.SaveChangesAsync();

                using (LogContext.PushProperty("LogEvent", Buttons.Add))
                {
                    Serilog.Log.Information("添加博客分类:[{title}]", model.Title);
                }
                AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.PostCategory));
                return(Json(AR));
            }

            if (id != im.Id)
            {
                AR.Setfailure("未发现此分类");
                return(Json(AR));
            }


            try
            {
                var model = await _context.PostCategories.FindAsync(id);

                model = _mapper.Map(im, model);

                if (User.Identity != null)
                {
                    model.UpdatedBy = User.Identity.Name;
                }
                model.UpdatedDate = DateTime.Now;
                _context.Update(model);
                await _context.SaveChangesAsync();

                var pm = new PageMeta
                {
                    Title       = im.SEOTitle,
                    Description = im.SEODescription,
                    Keywords    = im.SEOKeywords,
                    ModuleType  = (short)ModuleType.ARTICLECATEGORY,
                    ObjectId    = im.Alias
                };

                await CreatedUpdatedPageMetaAsync(_context, pm);

                using (LogContext.PushProperty("LogEvent", Buttons.Update))
                {
                    Serilog.Log.Information("修改博客分类:[{title}]", model.Title);
                }

                AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.PostCategory));
                return(Json(AR));
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostCategoryExists(im.Id))
                {
                    AR.Setfailure("未发现此分类");
                    return(Json(AR));
                }
                else
                {
                    AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.PostCategory));
                    return(Json(AR));
                }
            }
        }