Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
ProductController

C++
[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Product product, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
            Product p = new Product
            {
                Id = product.Id,
                Name = product.Name,
                Description = product.Description,
                Image = product.Image
            };

            if (file != null)
            {
                string Image = Path.Combine(Server.MapPath("~/Upload"), Path.GetFileName(file.FileName));
                file.SaveAs(Image);
                p.Image = "~/Upload/" + file.FileName;
            }

            db.Entry(p).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            return View(product);
        }

    }

    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Product product = db.Products.Find(id);
        if (product == null)
        {
            return HttpNotFound();
        }
        return View(product);
    }


Edit.cshtml

@using (Html.BeginForm("Edit",
                  "Products",
                  FormMethod.Post,
                  new { enctype = "multipart/form-data" }))
     {
   @Html.AntiForgeryToken()

   <div class="form-horizontal">

       @Html.ValidationSummary(true, "", new { @class = "text-danger" })
       @Html.HiddenFor(model => model.Id)

         <div class="form-group">
           @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
           <div class="col-md-10">
             <img src="@Url.Content(Model.Image)" width="150" />
           </div>
       </div>
       }


What I have tried:

I want to delete the picture with the button. How can I do it ? I can delete products but I can not delete pictures. I can delete products with Id. I tried to do examples on the internet but I could not. Would you make an illustrative example?

Thanks in advance for help.
Posted
Updated 18-Dec-17 3:13am
Comments
F-ES Sitecore 18-Dec-17 4:46am    
What do you mean by "picture"? What exactly do you want to delete?
Member 13582084 18-Dec-17 4:53am    
I have a product, there are 6 pictures. I just want to remove the picture from the site.

Did you mean just use edit action to change product's image to empty and delete the file without delete product?

if that's the case you could define a hidden input to store delete action

@Html.Hidden("deleteImage","false")

and define button as Delete Image

use javascript to set hidden input to true

<script>
function DeleteImage(){
   $("#deleteImage").val('true');
   //todo:Set image src to empty
}
</script>


and then when you click post to the Edit Action
public ActionResult Edit(Product product, HttpPostedFileBase file,bool deleteImage)
    {
        if (ModelState.IsValid)
        {
           if(deleteImage){
                 //todo:delete image by product.Image
                 p.Image="";
            }
        }
    }
 
Share this answer
 
sir If you would like to delete from the folder where you save bellow is the code

var filePath = Server.MapPath("~/Images/" + filename);
if(File.Exists(filePath))
{
    File.Delete(filePath);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900