如何使用(尝试)UpdateModel?

发布于 2024-11-27 01:12:26 字数 1496 浏览 1 评论 0原文

使用 (Try)UpdateModel 的正确方法是什么?

当我运行此命令时:

  • TryUpdateModel 返回 true,
  • ViewData 没有错误,
  • 但我的 < code>Proxy 未更新。

操作方法

public void Save(string TypeName, int Id, FormCollection idontknow) {
    var types = Assembly.GetExecutingAssembly().GetTypes();
    var ObjectType=(from t in types where t.Name == TypeName select t).First();
    var Proxy = context.Set(ObjectType).Find(Id); // EF 4.1
    if (TryUpdateModel(Proxy, TypeName)) {
        var x = ViewData.GetModelStateErrors(); // no errors
    }
}

发布数据

TypeName=Thing&Id=1&Thing.Id=1&Thing.Name=hello&Thing.OptionID=2

事物类

public class Thing : Base {
    public virtual Nullable<int> OptionID { get; set; }
    public virtual Option Option { get; set; }
    public virtual ICollection<ListItem> ListItems { get; set; }
}
public class Base {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    [NotMapped]
    public virtual int? EntityState { get; set; }
}

编辑: 我还尝试显式传递表单集合 TryUpdateModel(Proxy, TypeName, idontknow)

编辑 #2: (响应 NickLarsen)

  1. 重新启动 VS 和服务器,没有变化。
  2. 值实际上位于 FormCollection 中。
  3. 模拟数据有效!我知道我一定在这里搞砸了一些事情。
  4. 使用调试器检查值。

What is the right way to use (Try)UpdateModel?

When I run this:

  • TryUpdateModel returns true,
  • ViewData has no errors,
  • but my Proxy is not updated.

Action Method

public void Save(string TypeName, int Id, FormCollection idontknow) {
    var types = Assembly.GetExecutingAssembly().GetTypes();
    var ObjectType=(from t in types where t.Name == TypeName select t).First();
    var Proxy = context.Set(ObjectType).Find(Id); // EF 4.1
    if (TryUpdateModel(Proxy, TypeName)) {
        var x = ViewData.GetModelStateErrors(); // no errors
    }
}

Posted Data

TypeName=Thing&Id=1&Thing.Id=1&Thing.Name=hello&Thing.OptionID=2

Thing Class

public class Thing : Base {
    public virtual Nullable<int> OptionID { get; set; }
    public virtual Option Option { get; set; }
    public virtual ICollection<ListItem> ListItems { get; set; }
}
public class Base {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    [NotMapped]
    public virtual int? EntityState { get; set; }
}

EDIT: I also tried passing the form collection explicitly
TryUpdateModel(Proxy, TypeName, idontknow)

EDIT #2: (in response to NickLarsen)

  1. Restarted VS and server, no change.
  2. Values are actually in the FormCollection.
  3. Mock data works! I know I must be messing up something here.
  4. Using debugger to check values.

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

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

发布评论

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

评论(1

浅唱ヾ落雨殇 2024-12-04 01:12:26

我剥离了所有 EF 内容,并尝试获取该查询字符串来用值填充模型......并且它工作得很好。

//controller class
public ActionResult Save(string TypeName, int Id, FormCollection idontknow)
{
    var Proxy = new Thing
    {
        Id = 33,
        OptionID = 2234,
        Name = "tony",
    };
    if (TryUpdateModel(Proxy, TypeName))
    {
        ViewBag.Message = "WInner";
    }

    return RedirectToAction("Index");
}
//end controller class

public class Thing : Base
{
    public virtual Nullable<int> OptionID { get; set; }
}
public class Base
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

老实说,我无法想象你的代码中的哪些内容会阻止它工作,但我建议你一一浏览列表,并在每一步后进行测试......

  1. 保存你的进度并重新启动 VS 和你的开发服务器
  2. 检查值实际上是在表单数据中,也许有什么东西妨碍了那里。
  3. 像我一样模拟一些垃圾数据。 (检查问题是否与 EF 有关)
  4. 您如何确定代理没有更新?在调试器中、在页面上等等?
  5. 编辑您的问题并回答上述所有问题。

I stripped all the EF stuff and tried to get just that query string to populate the model with the values... and it worked just fine.

//controller class
public ActionResult Save(string TypeName, int Id, FormCollection idontknow)
{
    var Proxy = new Thing
    {
        Id = 33,
        OptionID = 2234,
        Name = "tony",
    };
    if (TryUpdateModel(Proxy, TypeName))
    {
        ViewBag.Message = "WInner";
    }

    return RedirectToAction("Index");
}
//end controller class

public class Thing : Base
{
    public virtual Nullable<int> OptionID { get; set; }
}
public class Base
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

Honestly I can't figure think of what in your code would keep it from working, but I would suggest going through the list 1 by one and testing after each step...

  1. Save your progress and restart VS and your development server
  2. Check that the values are actually in the form data, maybe something is getting in the way there.
  3. Mock up some trash data like I did. (checking if the problem has something to do with EF)
  4. How are you identifying that Proxy isn't being updated? In the debugger, on the page, etc?
  5. Edit your question with the answer to all of the above questions.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文