public async Task <IActionResult> Create([Bind("Name,Price,Description,AttachmentUpload")] CreateGoodViewModel model) { if (ModelState.IsValid) { var newGood = _mapper.Map <GoodModel>(model); newGood.CreationDate = DateTime.Now; _context.Goods.Add(newGood); _context.SaveChanges(); if (model.AttachmentUpload != null) { string extension = Path.GetExtension(model.AttachmentUpload.FileName); string path = "/Files/" + newGood.Id + extension; //var Stream = model.AttachmentUpload.OpenReadStream(); //this.ResizeAndSaveImage(Stream, _appEnvironment.WebRootPath + path); using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { await model.AttachmentUpload.CopyToAsync(fileStream); } newGood.Attachment = path; _context.SaveChanges(); } } else { var partialViewHtml = await this.RenderViewAsync(nameof(Create), model, true); TempData.Put(Constants.ERROR_MODAL, partialViewHtml); } return(RedirectToAction(nameof(Index))); }
public ActionResult Edit(CreateGoodViewModel viewModel, IEnumerable <HttpPostedFileBase> fileUpload) { var good = _mapper.Map <GoodDTO>(viewModel); foreach (var photo in fileUpload) { if (photo == null) { break; } string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Uploaded/"); string filename = Path.GetFileName(photo.FileName); string pathToFile = Path.Combine(path, filename); if (filename != null) { photo.SaveAs(pathToFile); } _photoService.AddOrUpdate(new PhotoDTO { GoodId = good.GoodId, PhotoPath = pathToFile }); } _goodService.AddOrUpdate(good); return(RedirectToAction("Index", "Good")); }
public ActionResult CreateGood(CreateGoodViewModel model) { if (ModelState.IsValid) { IBLL.IGoodsManager goodsManager = new GoodsManager(); goodsManager.AddGoods(model.Name, model.ImgsUrl, model.Price, model.PriceOld); return(RedirectToAction("GoodsList")); } ModelState.AddModelError("", "您录入的信息有误"); return(View(model)); }
public async Task <ActionResult> EditGood(CreateGoodViewModel model) { if (ModelState.IsValid) { IBLL.IGoodsManager goodsManager = new GoodsManager(); await goodsManager.EditGoods(model.Id, model.Name, model.ImgsUrl, model.Price, model.PriceOld); return(RedirectToAction("GoodsList")); } else { return(View(model)); } }
public async Task <IActionResult> CreateGood( [FromBody] CreateGoodViewModel goodVm) { if (ModelState.IsValid) { _context.Goods.Add(new Good { Name = goodVm.Name, Price = goodVm.Price }); await _context.SaveChangesAsync(); return(Ok()); } return(BadRequest()); }