I’ve been trying to update the products and add them onto the index page. However, there is something wrong with my viewmodels. I also cannot fetch other data from other viewmodels. The upsert option cannot be done. What are the possible reasons and how can I approach the problem? The list that you see doesn’t get updated.
Model:
public class ProductVM
{
public Product Product { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> CategoryList { get; set; }
}
View:
@using Jewelry.Models.ViewModels
@model ProductVM
<div class="card shadow border-0 my-4">
<div class="card-header bg-secondary bg-gradient ml-0 py-3">
<div class="row">
<div class="col-12 text-center">
<h2 class="text-white py-2">Ürün Oluştur</h2>
</div>
</div>
</div>
<div class="card-body p-4">
<form method="post" class="row" enctype="multipart/form-data">
<input asp-for="Product.Id" hidden />
<input asp-for="Product.ImageUrl" hidden />
<div class="row">
<div class="col-10">
<div class="border p-3">
<div class="row pb-2">
@* <h2 class="text-primary">Kategori Oluştur</h2> *@
</div>
@* <div asp-validation-summary="ModelOnly"></div> *@
<div class="form-floating py-2 col-12">
<input asp-for="Product.Title" class="form-control border-0 shadow" />
<label asp-for="Product.Title" class="ms-2"></label>
<span asp-validation-for="Product.Title" class="text-danger"></span>
</div>
<div class="py-2 col-12">
<label asp-for="Product.Description" class="ms-2"></label>
<textarea asp-for="Product.Description" class="form-control border-0 shadow"></textarea>
</div>
<div class="form-floating py-2 col-12">
<input asp-for="Product.Characteristics" class="form-control border-0 shadow" />
<label asp-for="Product.Characteristics" class="ms-2"></label>
<span asp-validation-for="Product.Characteristics" class="text-danger"></span>
</div>
<div class="form-floating py-2 col-12">
<input asp-for="Product.ListPrice" class="form-control border-0 shadow" />
<label asp-for="Product.ListPrice" class="ms-2"></label>
<span asp-validation-for="Product.ListPrice" class="text-danger"></span>
</div>
<div class="form-floating py-2 col-12">
<select asp-for="Product.CategoryId" asp-items="@Model.CategoryList" class="form-select border-0 shadow">
<option disabled selected>--Kategori Seç</option>
</select>
<label asp-for="Product.CategoryId" class="ms-2"></label>
<span asp-validation-for="Product.CategoryId" class="text-danger"></span>
</div>
<div class="form-floating py-2 col-12">
<input type="file" name="file" class="form-control border-0 shadow" />
<label asp-for="Product.ImageUrl" class=" ms-2"></label>
</div>
<div class="row">
<div class="col-6 col-md-3">
@if (Model.Product.Id != 0)
{
<button type="submit" class="btn btn-primary form-control">Güncelle</button>
}
else
{
<button type="submit" class="btn btn-primary form-control">Oluştur</button>
}
</div>
<div class="col-6 col-md-3">
<a asp-controller="Product" asp-action="Index" class="btn btn-outline-primary border form-control">
Listeye Geri Dön
</a>
</div>
</div>
</div>
</div>
<div class="col-2">
<img src="@Model.Product.ImageUrl" width="75%"
style="border-radius:5px; border:1px solid #bbb9b9" />
</div>
</div>
</form>
</div>
</div>
@section Scripts {
@{
<partial name="_ValidationScriptsPartial" />
}
}
Controller:
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, @"imagesproduct");
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 = @"imagesproduct" + 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);
}
}
I tried to change the viewmodel into the model of Product only but it turns out to be wrong because of I cannot change the controller.