i need help.
I want to update image in Edit page, but just when it is necessary.
the problem is whenever i do an edit i have to upload the image again.
i want to keep the old image if i do not want to update it.
hope you all understand me.
this is the view :
<div class="form-group">
@Html.LabelFor(model => model.PersonalImage, htmlAttributes: new { @class = "control-label " })
<div class="col-md-10">
<img src="~/Content/Uploads/Adherentes/Personalimages/@Html.DisplayFor(model => model.PersonalImage)" width="80" height="80" id="uploadperso" class="img-thumbnail" style="width: 160px; height: 160px; cursor: pointer"/>
<input onchange="showPhoto(this);" type="file" accept="Image/*" id="PersonalImage" name="PersonalImage" class="form-control" />
@Html.ValidationMessageFor(model => model.PersonalImage, "", new { @class = "text-danger" })
</div>
</div>
and this the update controller :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(HttpPostedFileBase PersonalImage, adherent adherent)
{
if (ModelState.IsValid)
{
string _ImageName = Path.GetFileName(PersonalImage.FileName);
string _Path = Path.Combine(Server.MapPath("~/Content/Uploads/Adherentes/Personalimages"), _ImageName);
PersonalImage.SaveAs(_Path);
adherent.PersonalImage = _ImageName;
_db.Entry(adherent).State = EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(adherent);
}
2
Answers
Hello i found out the solution.
Thank you for all who participated in this Thank you @Ramp2010
Assuming your database and controller doesn’t require an image. You can adjust your controller method to search for an image provided before writing.