0
    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?

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

error : Attaching an entity of type 'Solud.Models.page_image' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
0

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

1 Comment

error : Attaching an entity of type 'Solud.Models.page_image' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.