Home > Enterprise >  Update Image without losing the old path in Asp.net MVC
Update Image without losing the old path in Asp.net MVC

Time:08-24

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 >
                            @Html.LabelFor(model => model.PersonalImage, htmlAttributes: new { @class = "control-label  " })
                            <div >
                                <img  src="~/Content/Uploads/Adherentes/Personalimages/@Html.DisplayFor(model => model.PersonalImage)" width="80" height="80"  id="uploadperso"  style="width: 160px; height: 160px; cursor: pointer"/>
                                <input onchange="showPhoto(this);" type="file" accept="Image/*" id="PersonalImage" name="PersonalImage"  />

                                @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);
    }

CodePudding user response:

Assuming your database and controller doesn't require an image. You can adjust your controller method to search for an image provided before writing.

if (ModelState.IsValid)
    {
       if(PersonalImage != null){

            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");
    }

CodePudding user response:

Hello i found out the solution.

 if (ModelState.IsValid)
        {
            var data =  _db.adherents.AsNoTracking().Where(x => x.Id == adherent.Id).FirstOrDefault();

            string PersonalImagePath = data.PersonalImage;

            data = null;
            if (PersonalImage != null && PersonalImage.ContentLength > 0 )
            {
                string _ImageName = Path.GetFileName(PersonalImage.FileName);
                string _Path = Path.Combine(Server.MapPath("~/Content/Uploads/Adherentes/Personalimages"), _ImageName);
                PersonalImage.SaveAs(_Path);
                adherent.PersonalImage = _ImageName;

            }
            else
            {
                adherent.PersonalImage = PersonalImagePath;
            }
                    
            
            
            _db.Entry(adherent).State = EntityState.Modified;
            _db.SaveChanges();


        

Thank you for all who participated in this Thank you @Ramp2010

  • Related