MVC 的 UpdateModel 表现得很奇怪并且正在恢复值。为什么?

发布于 2024-11-14 19:50:07 字数 1225 浏览 2 评论 0原文

我有一个对象正在尝试使用 MVC(2) 进行更新,也使用 EntityFramework 框架。

我知道我没有提供太多代码,但我真的觉得没有必要。我有这样的逻辑的原因,我将解释:

  • 我有一些级联下拉菜单,当其中一个下拉菜单为空时,我使用 jQuery 用“未知”值填充它,例如使用像 -1 这样的 id。
  • 因此,当我得到值 -1 时,我会创建另一个表中的 UNKNOWN 值。
  • 然后我找到该对象并将其分配给 Fruit

代码如下:

if (id == -1)
            {
                //The object was unknown so create it
                var newUnknown = new Fruit
                                 {
Name = "UNKNOWN";
};

EntityFramework.AddToFruits(newUnknown);
EntityFramework.SaveChanges();
defaultValueObject = EntityFramework.Fruits.Single(x=>x.FruitID == newUnknown.FruitID);

object.Fruit = defaultValueObject;
object.Date = DateTime.Now;

UpdateModel(object);
EntityFramework.SaveChanges();

UpdateModel(object); 行运行后,我设置的值,例如 Fruit 恢复为从表单发送过来的内容...(即 -1),然后 EntityFramework.SaveChanges(); 失败 FK 约束(因为 id 为 -1 的 Fruit 不会失败)不存在)!很公平——但这不是我分配给你的!

我不明白为什么它会恢复,因为在第一个 AddToFruits() 之后,未知数就在数据库中......直到 UpdateModel(object); 它位于 object 中...

如果它像我分配的那样添加它,则不会出现 FK 约束异常。但是 MVC 的 UpdateModel 决定做一些奇怪的事情并默认(可能是表单提交时发生的事情)并将其搞砸。

为什么会出现这种情况?我该如何修复它?

I have an object that I am trying to update using MVC(2), also using EntityFramework framework.

I know I haven't provided much code but don't really feel it necessary. The reason I have logic like this, i'll explain:

  • I have some cascading drop downs, and when one of the drop downs is empty I use jQuery to fill it with an "UNKNOWN" value e.g. with id like -1.
  • So when I get value -1, I create the UNKNOWN value which is in another table.
  • Then I find that object and assign it to Fruit

Code like this:

if (id == -1)
            {
                //The object was unknown so create it
                var newUnknown = new Fruit
                                 {
Name = "UNKNOWN";
};

EntityFramework.AddToFruits(newUnknown);
EntityFramework.SaveChanges();
defaultValueObject = EntityFramework.Fruits.Single(x=>x.FruitID == newUnknown.FruitID);

object.Fruit = defaultValueObject;
object.Date = DateTime.Now;

UpdateModel(object);
EntityFramework.SaveChanges();

After UpdateModel(object); line is run, the value I set in, for example, Fruit reverts to what was sent over from the form... (which is -1) and then EntityFramework.SaveChanges(); fails FK contrainst (because fruit with id -1 doesn't exist)! Fair enough - but that's not what I assigned to you!

I don't understand why it reverts, because after the first AddToFruits() the unknown is in the database fine... and all up untill UpdateModel(object); it is in object...

If it adds it like I have assigned it there will be no FK contraint exception. But MVC's UpdateModel decides to do something strange and default to (perhaps what came over with form submission) and screws it up.

Why does this happen? How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救星 2024-11-21 19:50:07

UpdateModel 并没有做任何奇怪的事情,它正在做它应该做的事情;使用表单提交中的值更新对象。

EF 正在跟踪更改,因此它尝试使用对象在保存时拥有的值来更新数据库值。

通过在运行 UpdateModel 后进行更改来解决该问题。

UpdateModel is not doing anythign strange, it's doing what it's supposed to do; update an object with the values from the form submission.

EF is tracking changes, so it tries to update the db values with what the object has at the time of save.

Solve it by making your changes after running UpdateModel.

喜爱皱眉﹌ 2024-11-21 19:50:07

您可以将字符串数组传递给 UpdateModel,告诉它要更新模型中的哪些属性。

public class Fruit
{
    int ID { get; set; }
    bool IsTasty { get; set; }
    string MyOtherPropert { get; set; }
    DateTime Date { get; set; }
}

...

if (id == -1)
{
    //The object was unknown so create it
    var newUnknown = new Fruit
    {
        Name = "UNKNOWN"
    };
    EntityFramework.AddToFruits (newUnknown);
    EntityFramework.SaveChanges ();

    defaultValueObject = EntityFramework.Fruits.Single (x=>x.FruitID == newUnknown.FruitID);
    defaultValueObject.Date = DateTime.Now;

    UpdateModel (object, new string[] { "IsTasty", "MyOtherProperty" });

    EntityFramework.SaveChanges ();
}

这样做将确保您想要更新的所有属性都会被更新,并且您想要保留的属性不会被触及。在这种情况下,UpdateModel 将忽略 ID 和日期。

You can pass in an array of strings to UpdateModel to tell it what properties to update in your model.

public class Fruit
{
    int ID { get; set; }
    bool IsTasty { get; set; }
    string MyOtherPropert { get; set; }
    DateTime Date { get; set; }
}

...

if (id == -1)
{
    //The object was unknown so create it
    var newUnknown = new Fruit
    {
        Name = "UNKNOWN"
    };
    EntityFramework.AddToFruits (newUnknown);
    EntityFramework.SaveChanges ();

    defaultValueObject = EntityFramework.Fruits.Single (x=>x.FruitID == newUnknown.FruitID);
    defaultValueObject.Date = DateTime.Now;

    UpdateModel (object, new string[] { "IsTasty", "MyOtherProperty" });

    EntityFramework.SaveChanges ();
}

Doing this will make sure all the properties you want updated will be, and those you want left alone won't be touched. In this case, ID and Date are going to be ignored by UpdateModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文