I working on CRUD of Product. My problem is when I update any product and not import the image, its lose all the image of product. Here my controller method Edit
of Product
:
public async Task<IActionResult> Edit(int? id)
{
if (id == null || _context.Products== null)
{
return NotFound();
}
var product = await _context.Products.FindAsync(id);
if (product == null)
{
return NotFound();
}
return View(product);
}
// POST: Admin/Products/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(int id, Product product)
{
if (id != product.Id)
{
return NotFound();
}
string uniqueFileName1 = GetProfilePhotoFileName1(product);
product.Image1 = uniqueFileName1;
string uniqueFileName2 = GetProfilePhotoFileName2(product);
product.Image2 = uniqueFileName2;
string uniqueFileName3 = GetProfilePhotoFileName3(product);
product.Image3 = uniqueFileName3;
string uniqueFileName4 = GetProfilePhotoFileName4(product);
product.Image4 = uniqueFileName4;
_context.Update(product);
_context.SaveChanges();
return View(product);
}
I want if I update the product but not import any new images they will be the same images when I created. But now when I click edit and save its just lose all the url of img in database.
Here my view of edit:
Does anyone know the problem and how to fix it? Many thanks guys for reading