skip to Main Content

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


  1. Chosen as BEST ANSWER

    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


  2. 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");
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search