コード例 #1
0
        public JsonResult EditGoods(EditGoodsViewModel model)
        {
            CustomJsonResult reuslt = new CustomJsonResult();

            model.Product.ElseImgUrls = Newtonsoft.Json.JsonConvert.SerializeObject(model.ElseImgUrls);
            reuslt = BizFactory.Product.Edit(this.CurrentUserId, model.Product);

            return(reuslt);
        }
コード例 #2
0
        // GET: GoodsManagement/EditGoods/5
        public async Task <IActionResult> EditGoods(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var goods = await _context.Goods.Include(G => G.ItemsGoods)
                        .ThenInclude(IG => IG.Item)
                        .SingleOrDefaultAsync(m => m.Id == id);

            if (goods == null)
            {
                return(NotFound());
            }

            var categorySelectListItems = from category in _context.GoodsCategories
                                          orderby category.SerialCode
                                          let selected = category.Id == goods.CategoryId
                                                         select new SelectListItem
            {
                Text     = $"{category.Name}({category.SerialCode})",
                Value    = category.Id.ToString(),
                Selected = selected
            };

            ViewBag.Categories = await categorySelectListItems.ToListAsync();

            var viewModel = new EditGoodsViewModel
            {
                Id          = goods.Id,
                SerialCode  = goods.SerialCode,
                Name        = goods.Name,
                Price       = goods.Price,
                Description = goods.Description?.Replace("<br/>", Environment.NewLine),
                ImagePath   = goods.Image
            };

            viewModel.ItemGoodsList.AddRange(goods.ItemsGoods.OrderByDescending(IG => IG.Item.Category).ThenBy(IG => IG.Probability));

            return(View(viewModel));
        }
コード例 #3
0
        public async Task <IActionResult> EditGoods(Guid id, [FromForm] EditGoodsViewModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            var goods = await _context.Goods.SingleOrDefaultAsync(m => m.Id == id);

            if (goods == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (model.Image?.Length > 0)
                    {
                        var dirPath = Path.Combine(_hostingEnvironment.WebRootPath, _appOptions.GoodsImagePath);
                        var dirInfo = new DirectoryInfo(dirPath);
                        if (dirInfo.Exists != true)
                        {
                            dirInfo.Create();
                        }

                        var fileName     = Path.GetFileName(model.Image.FileName);
                        var fileFullPath = Path.Combine(dirInfo.FullName, fileName);
                        using (var stream = new FileStream(fileFullPath, FileMode.Create))
                        {
                            await model.Image.CopyToAsync(stream);
                        }

                        // Update goods image
                        goods.Image     = "/" + Path.Combine(_appOptions.GoodsImagePath, fileName);
                        model.ImagePath = goods.Image;
                    }

                    // Update goods info
                    goods.SerialCode  = model.SerialCode;
                    goods.Name        = model.Name;
                    goods.Price       = model.Price;
                    goods.Description = model.Description?.Replace(Environment.NewLine, "<br/>");
                    goods.CategoryId  = Guid.Parse(model.CategoryId);

                    _context.Update(goods);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!GoodsExists(model.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
コード例 #4
0
        public ViewResult EditGoods(int id)
        {
            EditGoodsViewModel model = new EditGoodsViewModel(id);

            return(View(model));
        }