ASP.NET MVC Image edit

    public ActionResult Edit([Bind(Include = "id,category,title,image,active,ImageFile")] page_image page_image)
    {
        if (ModelState.IsValid)
        {
            if (page_image.ImageFile != null)
            {
                string fileName = Path.GetFileNameWithoutExtension(page_image.ImageFile.FileName);
                string extension = Path.GetExtension(page_image.ImageFile.FileName);
                fileName = fileName + DateTime.Now.ToString("yymmssff") + extension;
                page_image.image = "/Uploads/page_images/" + fileName;

                fileName = Path.Combine(Server.MapPath("/Uploads/page_images"), fileName);
                page_image.ImageFile.SaveAs(fileName);
            }
                            
            db.Entry(page_image).State = EntityState.Modified;
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        ViewBag.category = new SelectList(db.page, "id", "title", page_image.category);
        return View(page_image);
    }

Here I'm able to edit the User but is not showing the previous Image so If I click submit with out loading a new Image it will delete the previous one. What I have to do is the Edit view, I want it to show the name of the image. Can you guide me to the right direction?

Upvotes: 0

Views: 103

Answers (2)

Ram Anugandula
Ram Anugandula

Reputation: 614

If page_image.Image is null then get previous image and assign

 if (page_image.ImageFile != null)
 {
       string fileName = Path.GetFileNameWithoutExtension(page_image.ImageFile.FileName);
       string extension = Path.GetExtension(page_image.ImageFile.FileName);
       fileName = fileName + DateTime.Now.ToString("yymmssff") + extension;
       page_image.image = "/Uploads/page_images/" + fileName;

       fileName = Path.Combine(Server.MapPath("/Uploads/page_images"), fileName);
       page_image.ImageFile.SaveAs(fileName);

 } else {
    // get existing image from database 
     var data = db.page_image.AsNoTracking().Where(b => b.id == page_image.id).FirstOrDefault();
     //assign to existing image
     page_image.image = data.image ;

 }
 db.Entry(page_image).State = EntityState.Modified;
 db.SaveChanges();
 return RedirectToAction("Index");

Update : Error is throwing here because of same instance of dbcontext. You can create new instance to fetch or do as following.

var data = db.page_image.AsNoTracking().Where(b => b.id == page_image.id).FirstOrDefault();

or

using (var db2 = new YourDbContext())
{
    var data = db2.page_image.Where(b => b.id == page_image.id).FirstOrDefault();
}

Upvotes: 0

Shrembo
Shrembo

Reputation: 1011

The problem is because you save the view model as is to the Database, you should have a DTO. Anyway, try to get the ImageFile from the DB again, in case it submitted null.

if(page_image.ImageFile != null)
{
    //  your uploading logic
}
else
{
    var oldData = db.Set<page_image>().Where(x => x.id == page_image.id).FirstOrDefault();
    page_image.ImageFile = oldData.ImageFile;
}

Upvotes: 2

Related Questions