1

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?

4

3 回答 3

0

看来您的项目对象未附加到上下文试试这个

            if (file1 != null)
            {
                var path = FileUploadHelper.UploadFile(file1, Server.MapPath(ProjectImages));

                var image = new Image { ImageName = file1.FileName, Path = path, ContentType = file1.ContentType };
                db.Project.Attach(project);
                db.Images.Add(image);                    
                project.MainImage = image;
                context.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
            }

并在您的 POST 方法结束时删除它:

            db.Entry(project).State = EntityState.Modified;
            db.SaveChanges();
于 2013-07-11T19:35:09.340 回答
-2

达斯,我以前处理过这个问题,这是 MVC 的一个疯狂的怪癖。您正在 post 方法中更改模型并将其返回到同一视图。它不会在不先清除值的情况下将新值传递回视图。

ModelState.Remove("MainImage");
ModelState.SetValue("MainImage", imageEntity);

或者

ModelState.Clear();

然后完全重建模型。我知道这听起来很疯狂,但是,在那里,做到了,在这种情况下没有其他方法可以工作。

于 2013-07-11T19:19:53.190 回答
-2

禁用 MVC 和浏览器中的自动缓存,以便每次都能更新到最新版本。

您可以使用 OutputCacheAttribute 来控制服务器和/或浏览器对特定操作或控制器中所有操作的缓存。

禁用控制器中的所有操作:

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will be applied to all actions in MyController, unless those actions override with their own decoration
public class MyController : Controller
{
  // ... 
}

禁用特定操作:

public class MyController : Controller
{
    [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will disable caching for Index only
    public ActionResult Index()
    {
       return View();
    }
} 
于 2013-07-11T19:40:38.627 回答