Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading