C# 更新模型并向模型添加新对象

C# 更新模型并向模型添加新对象,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有一个实体如下: class Project{ public int Id {set;get;} ... some other props public virtual Image Image {set;get;} } 在控制器上,我具有以下功能: [HttpPost] public ActionResult Edit(Project project, HttpPostedFileBase file1) {

我有一个实体如下:

class Project{
   public int Id {set;get;}
   ... some other props
   public virtual Image Image {set;get;}
}
在控制器上,我具有以下功能:

       [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);
        }
这里的问题是,当我阅读这个记录的项目时,我仍然看到旧的图像。ie:它不会更新给定项目实体的映像

为什么会这样


我可以从DB加载项目并为其赋值。但这为什么不起作用呢?我能在不从db加载对象的情况下让它工作吗?

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

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


然后完全重建模型。我知道这听起来很疯狂,但是,在那里,这样做了,在这个场景中没有其他任何东西可以工作。

您的项目对象似乎没有附加到上下文 试试这个

并在POST方法的末尾删除此项:

            db.Entry(project).State = EntityState.Modified;
            db.SaveChanges();

在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();
    }
} 
禁用特定操作:

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

是否将图像添加到数据库中?
项目的其他属性是否得到更新?将
image
(不是
imageEntity
)变量分配给
MainImage
属性并调用
db.SaveChanges()
一次如何?Store update、insert或delete语句会影响意外的行数(0)。自加载实体后,实体可能已被修改或删除。刷新ObjectStateManager条目。这是我在你的建议后得到的。这是因为我上传的新图像没有
Id
。我猜EF知道插入和更新的顺序,问题不是从这里来的。您有任何并发检查吗?如何使用EF?代码优先?数据库优先?先建模?试着这样做进行测试,而不是将项目附加到上下文中,从数据库中获取项目并更新获取的项目。你在说什么我很惊讶这会失败。图像不会返回到模型中的原因是,在post方法中,必须清除模型并重建它,或者清除模型中的单个字段并为其指定新值。MVC中的一个怪癖是,如果你不这样做,它将保持其在文章之前的价值。