【问题标题】:MVC3 with EF 4.1 and EntityState.Modified on updateMVC3 与 EF 4.1 和 EntityState.Modified 更新
【发布时间】:2011-12-02 09:23:06
【问题描述】:

在使用 .NET MVC3 更新对象时,我无法理解 EntityState.Modified。

我有一个模型在上传图像时存储 ImageFilePath 和 ImageContentType。这是创建操作的样子。

    [HttpPost]
    public ActionResult Create(SneakPeekCollection collection, HttpPostedFileBase image)
    {
        try
        {
            if (image != null)
            {
                var filepath = Path.Combine(HttpContext.Server.MapPath("../../Uploads"), Path.GetFileName(image.FileName));
                image.SaveAs(filepath);
                collection.ImageContentType = image.ContentType;
                collection.ImageFilePath = "~/Uploads/" + image.FileName;

            }
            _db.SneakPeekCollections.Add(collection);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

在尝试编辑并随后更新此对象时出现问题。这是我的编辑操作。

    [HttpPost]
    public ActionResult Edit(int id, SneakPeekCollection collection, HttpPostedFileBase image)
    {
        try
        {
            if (image != null)
            {
                var filepath = Path.Combine(HttpContext.Server.MapPath("../../../Uploads"), Path.GetFileName(image.FileName));
                image.SaveAs(filepath);
                collection.ImageContentType = image.ContentType;
                collection.ImageFilePath = "~/Uploads/" + image.FileName;
            }
            _db.Entry(collection).State = EntityState.Modified;
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

我相信问题出在我设置 EntityState.Modified 将所有属性标记为已修改的事实。如果我不上传新图像,则来自前端的 ImageFilePath 和 ImageContentType 实际上为 null,这就是要存储的内容。

我的问题是如何解决这个问题?使用 EntityState.Modified 的正确方法是什么?

【问题讨论】:

  • 您的问题解决了吗?你用了什么解决方案?请告诉我。

标签: .net asp.net-mvc-3 entity-framework


【解决方案1】:

【讨论】:

  • 嘿佩雷茨。谢谢你。我阅读了您的链接,并且能够解决我需要在编辑表单帖子中维护 CreatedOn 值的问题。我所做的是,在编辑帖子中,我获取了现有行,并从该数据中获得了 CreratedOn 值并将其设置为模型对象在帖子中......对吗?你说什么?
【解决方案2】:

您可以从数据库中检索模型并使用 UpdateModel 获取新值(如果存在),而不是通过在参数中接受 SneakPeakCollection 来使用隐式模型绑定。像这样的:

var collection = _db.SneakPeaks.Find(id); // Get the entity to update from the db
UpdateModel(collection); // Explicitly invoke model binding
if (image != null)
{
                var filepath = Path.Combine(HttpContext.Server.MapPath("../../../Uploads"), Path.GetFileName(image.FileName));
                image.SaveAs(filepath);
                collection.ImageContentType = image.ContentType;
                collection.ImageFilePath = "~/Uploads/" + image.FileName;
}
_db.SaveChanges();

【讨论】:

    【解决方案3】:

    在您提交时,您需要检查模型是否有效,然后运行您的 CRUD 例程。

    if(ModelState.IsValid)
    {
       // Save my model
    }
    

    【讨论】:

    • 这没有效果。该对象保存得很好,但就像我提到的那样,它清除了 ImageFilePath 和 ImageContentType 属性,因为它们没有显式设置并且 EntityState.Modified 已被调用。
    • 你能发布你的 SneakPeak 类定义吗?
    猜你喜欢
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多