Esempio n. 1
0
        // GET: Admin/DocCategories/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            var vm = new DocCategoryIM
            {
                Active     = true,
                Importance = 0
            };

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

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

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

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

            if (pm != null)
            {
                model.SEOTitle       = pm.Title;
                model.SEOKeywords    = pm.Keywords;
                model.SEODescription = pm.Description;
            }

            return(View(model));
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit([Bind("Id,Title,Alias,Description,Importance,Active,SEOTitle,SEOKeywords,SEODescription")] DocCategoryIM im, int id = 0)
        {
            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return(Json(AR));
            }

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

                await _context.SaveChangesAsync();

                // return RedirectToAction(nameof(Index));

                AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.DocCategory));
                return(Json(AR));
            }

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


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

                model = _mapper.Map(im, model);

                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);

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