I have an entity as follows:
class Project{
public int Id {set;get;}
... some other props
public virtual Image Image {set;get;}
}
on controller I have the following:
[HttpPost]
public ActionResult Edit(Project project, HttpPostedFileBase file1)
{
if (ModelState.IsValid)
{
if (file1 != null)
{
var path = FileUploadHelper.UploadFile(file1, Server.MapPath(ProjectImages));
var image = new Image { ImageName = file1.FileName, Path = path, ContentType = file1.ContentType };
var imageEntity = db.Images.Add(image);
db.SaveChanges();
project.MainImage = imageEntity;
}
else
{
ModelState.AddModelError("FileEmpty", "You need to upload a file.");
return View(project);
}
db.Entry(project).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
Problem here is when I read the Project for this record, i still see the old image. ie: it is not updating the image for a given project entity.
Why is this happening?
I can load the project from DB and assign the value to it. but why this is not working? can i get it working without loading the object from db?