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

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

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

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

            model.Products = !string.IsNullOrEmpty(model.RelatedProducts) ? model.RelatedProducts.Split("|") : null;;

            var pm = await _context.PageMetas.FirstOrDefaultAsync(d => d.ModuleType == (short)ModuleType.SOLUTION && d.ObjectId == category.Id.ToString());

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

            var categories = await _context.Products.AsNoTracking()
                             .OrderByDescending(d => d.Importance).ToListAsync();

            ViewData["Products"] = new SelectList(categories, "Id", "Title");


            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> Edit([Bind("Id,Title,SubTitle,Thumbnail,ImageUrl,Body,Description,RelatedProducts,Products,Importance,Active,SEOTitle,SEOKeywords,SEODescription")] SolutionIM im, int id = 0)
        {
            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return(Json(AR));
            }

            im.RelatedProducts = im.Products != null?string.Join("|", im.Products) : null;;

            if (id == 0)
            {
                var model = _mapper.Map <Solution>(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.Solution));
                return(Json(AR));
            }
            else
            {
                if (id != im.Id)
                {
                    AR.Setfailure("未发现此分类");
                    return(Json(AR));
                }


                try
                {
                    var model = await _context.Solutions.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.SOLUTION,
                        ObjectId    = im.Id.ToString()
                    };

                    await CreatedUpdatedPageMetaAsync(_context, pm);

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