ASP.NET MVC 2 - "输入“XYZ”的模型无法更新"使用的UpdateModel和LINQ时实体(.NET 3.5) [英] ASP.NET MVC 2 - "The model of type 'XYZ' could not be updated" when using UpdateModel and LINQ to Entities (.NET 3.5)

查看:220
本文介绍了ASP.NET MVC 2 - "输入“XYZ”的模型无法更新"使用的UpdateModel和LINQ时实体(.NET 3.5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型设置使用LINQ到实体,并有code工作如预期,增加了数据库。但是,我不能得到的UpdateModel当我使用.NET 3.5的工作。

I have a model set up using LINQ to Entities and have code working that adds to the database as expected. However, I can't get UpdateModel to work when I am using .NET 3.5.

[HttpPost]
public ActionResult Edit(Site.Models.XYZ xyz)
{
    try
    {
        var original = db.XYZ.First(u => u.id == xyz.id);
        UpdateModel(original);
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    catch (Exception ex)
    {
        return View("Error");
    }
}

这将导致以下异常:

System.InvalidOperationException was caught
  Message=The model of type 'Site.Models.XYZ' could not be updated.
  Source=System.Web.Mvc
  StackTrace:
       at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
       at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix)
       at Site.Controllers.XYZController.Edit(Site.Models.XYZ xyz) in D:***.cs:line 81
  InnerException: 

如果我的UpdateModel(XYZ)例外不会发生,但数据不会保存任何

If I do UpdateModel(xyz) the exception does not occur, but the data does not save either.

我怎样才能获得的UpdateModel与这个工作(无更新到.NET 4.0),为什么不能被更新(例外是没有帮助的,因为没有内部异常)?

How can I get UpdateModel to work with this (without updating to .NET 4.0), why can't it be updated (exception is not helpful as there is no inner exception)?

推荐答案

设法解决这个问题。可以以两种方式之一来进行:

Managed to solve the problem. Can be done in one of two ways:

TryUpdateModel(original)

db.ApplyPropertyChanges(original.EntityKey.EntitySetName, xyz)

不知道为什么 TryUpdateModel 将工作,但的UpdateModel 不会。也许只是在.NET 3.5中的错误。

No idea why TryUpdateModel will work but UpdateModel won't. Maybe just a bug in .NET 3.5.

这篇关于ASP.NET MVC 2 - "输入“XYZ”的模型无法更新"使用的UpdateModel和LINQ时实体(.NET 3.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆