public IActionResult Create(AddProductFormModel model) { var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads"); string image = "default.jpg"; if (model.Image != null) { if (model.Image.Length > 0) { var uniqueFileName = this.GetUniqueName(model.Image.FileName); var filePath = Path.Combine(uploads, uniqueFileName); model.Image.CopyTo(new FileStream(filePath, FileMode.Create)); image = uniqueFileName; } } var result = this._products.Create(model.Title, model.Price, model.Description, image, model.CategoryId); if (result == 0) { TempData.AddErrorMessage("Неуспешно добавен продукт."); return(RedirectToAction("Index", "Home", new { area = "" })); } TempData.AddSuccessMessage("Успешно добавен продукт."); return(RedirectToAction("Index", "Home", new { area = "" })); }
public ActionResult AddProduct(AddProductFormModel inputModel) { if (ModelState.IsValid) { var domainModel = Mapper.Map <ProductDomainModel>(inputModel); product.Add(domainModel); return(RedirectToAction("Index")); } var viewModel = Mapper.Map <AddProductViewModel>(inputModel); IEnumerable <CategoryDomainModel> categories = category.Get(); viewModel.Categories = Mapper.Map <List <CategoryViewModel> >(categories); return(View(viewModel)); }