How to delete image?

时间:2017-12-18 07:23:09

标签: javascript asp.net-mvc

ProductsController

        [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>
        }

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.

0 个答案:

没有答案