
Невозможно выполнить опцию upsert. Каковы возможные причины и как я могу решить проблему? Список, который вы видите, не обновляется.
Модель:
Код: Выделить всё
public class ProductVM
{
public Product Product { get; set; }
[ValidateNever]
public IEnumerable CategoryList { get; set; }
}
Код: Выделить всё
@using Jewelry.Models.ViewModels
@model ProductVM
Ürün Oluştur
@* Kategori Oluştur *@
@* *@
--Kategori Seç
@if (Model.Product.Id != 0)
{
Güncelle
}
else
{
Oluştur
}
Listeye Geri Dön
[img]@Model.Product.ImageUrl[/img]
style="border-radius:5px; border:1px solid #bbb9b9" />
@section Scripts {
@{
}
}
Код: Выделить всё
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)
{
return View(productVM);
}
else
{
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");
if (!string.IsNullOrEmpty(productVM.Product.ImageUrl))
{
var oldImagePath = Path.Combine(wwwRootPath, productVM.Product.ImageUrl.TrimStart('\\'));
if (System.IO.File.Exists(oldImagePath))
{
System.IO.File.Delete(oldImagePath);
}
}
using (var fileStream = new FileStream(Path.Combine(productPath, fileName), FileMode.Create))
{
file.CopyTo(fileStream);
}
productVM.Product.ImageUrl = @"\images\product\" + fileName;
}
if (productVM.Product.Id == 0)
{
_unitOfWork.Product.Add(productVM.Product);
}
else
{
_unitOfWork.Product.Update(productVM.Product);
}
_unitOfWork.Save();
TempData["success"] = "Ürün başarılı bir şekilde oluşturuldu.";
return RedirectToAction("Index");
}
else
{
productVM.CategoryList = _unitOfWork.Category.GetAll().Select(u => new SelectListItem
{
Text = u.Name,
Value = u.Id.ToString()
});
return View(productVM);
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... hould-i-do
Мобильная версия