EN VI

C# - Update product but lose image of product ASP.NET Core?

2024-03-10 22:00:05
How to C# - Update product but lose image of product ASP.NET Core

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:

edit view

Does anyone know the problem and how to fix it? Many thanks guys for reading

Solution:

Simply wrap a conditional around the code that overrides the images, checking whether the first image was set. If so, do the overriding. Ignore it if not.

// 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 image1 = GetProfilePhotoFileName1(product);

    if (!String.IsNullOrEmpty(image1)) {
        string uniqueFileName1 = image1;
        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);
}
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login