ProductController.cs
Код: Выделить всё
public IActionResult Upsert(int? id)
{
ProductVM productVM = new()
{
CategoryList = _unitOfWork.Category.GetAll().Select(u => new SelectListItem
{
Text = u.Name,
Value = u.Id.ToString()
}),
Product = new Product()
};
if(id == null || id == 0)
{
//create
return View(productVM);
}
else
{
//update
productVM.Product = _unitOfWork.Product.Get(u => u.Id == id);
return View(productVM);
}
}
[HttpPost]
public IActionResult Upsert(ProductVM productVM,IFormFile? file)
{
if (ModelState.IsValid)
{
string wwwRootPath = _webHostEnvironment.WebRootPath;
if(file != null)
{
string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
string productPath = Path.Combine(wwwRootPath, @"images\product");
using (var fileStream = new FileStream(Path.Combine(productPath, fileName),FileMode.Create))
{
file.CopyTo(fileStream);
}
productVM.Product.ImageURL = @"\images\product\" + fileName;
}
_unitOfWork.Product.Add(productVM.Product);
_unitOfWork.Save();
TempData["success"] = "Product created successfully";
return RedirectToAction("Index");
}
else
{
productVM.CategoryList = _unitOfWork.Category.GetAll().Select(u => new SelectListItem
{
Text = u.Name,
Value = u.Id.ToString()
});
return View(productVM);
}
}
Я пробовал разные варианты, но он не работает
Я хочу показать img моего продукта на главной странице означает uspert.cshtml, но img не отображаетсяg
Подробнее здесь: https://stackoverflow.com/questions/791 ... -img-issue
Мобильная версия