简体   繁体   English

ASP.NET MVC UpdateModel意外失败

[英]ASP.NET MVC UpdateModel failing unexpectedly

I am trying to update some object properties by calling UpdateModel(myObject, new[] { "stringprop1", "stringprop2", "intprop"}) . 我试图通过调用UpdateModel(myObject, new[] { "stringprop1", "stringprop2", "intprop"})来更新一些对象属性。

This is failing for an unknown reason. 由于未知原因,此操作失败。 Several string properties are updating successfully. 几个字符串属性正在成功更新。 When I try to update an int property it fails. 当我尝试更新int属性时,它会失败。 The new int value is being sent from an html select list on the view. 新的int值是从视图上的html选择列表发送的。

I can see the data from the view in my ValueProvider. 我可以从ValueProvider的视图中查看数据。 The key matches the object's property name and the value is what I am expecting from the view (except that it's a string. I thought UpdateModel could handle simple type conversions. 密钥与对象的属性名称匹配,并且值是我从视图中期望的值(除了它是字符串。我认为UpdateModel可以处理简单的类型转换。

When I call UpdateModel an InvalidOperationException is thrown, ONLY IF THE PROPERTY'S VALUE IS CHANGING. 当我调用UpdateModel时,仅当属性值发生更改时,才会引发InvalidOperationException。

Also, the object is a Linq To Sql generated object. 同样,该对象是Linq To Sql生成的对象。

EDIT 编辑

Looking through Model State after calling TryUpdateModel, I see this exception on the property that I am having trouble with: 在调用TryUpdateModel之后浏览模型状态,我在遇到问题的属性上看到此异常:

- Exception {"Operation is not valid due to the current state of the object."} System.Exception {System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException}

The value I am trying to update is a foreign key. 我尝试更新的值是外键。 Why can't I update this value? 为什么我不能更新此值? Thanks for any help? 谢谢你的帮助?

EDIT 2 编辑2

I found this: http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/f9c4a01a-195a-4f2b-a1cb-e2fa06e28b25 我发现了这一点: http : //social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/f9c4a01a-195a-4f2b-a1cb-e2fa06e28b25

I could have sworn that I have changed the key instead of the association property before, but whatever. 我可能发誓说我以前更改过键而不是关联属性,但是无论如何。 What can be done in this MVC Model Binding situation? 在这种MVC模型绑定情况下可以做什么?

Screw it. 算了。 I just did it the old fashioned way: 我只是用老式的方式做的:

myObject.Skunk = db.Skunks.Single(s => s.ID == intprop);

I had this same problem and then realized it was the DataContext itself. 我遇到了同样的问题,然后意识到这是DataContext本身。 I was saving this in session, rather than creating a new one with each request. 我将其保存在会话中,而不是为每个请求创建一个新请求。 Once I created the Context for each request, this error went away. 为每个请求创建上下文后,该错误就消失了。

I came across this problem when I was using DataLoadOptions. 我在使用DataLoadOptions时遇到了这个问题。 Simply having DLO set for the column with foreign key was enough to cause this error. 只需为具有外键的列设置DLO就足以导致此错误。 I fixed it by making a new instance of DataContext without DLO. 我通过制作没有DLO的DataContext新实例来修复它。

Note, that it made no difference that the value in the column with foreign key was set to null at the beginning of update. 请注意,在更新开始时,具有外键的列中的值设置为null没什么区别。

I hope this helps someone who has a similar problem (this question was one of the first results that I came across when I was trying to solve it myself). 我希望这对遇到类似问题的人有所帮助(这个问题是我自己尝试解决该问题时遇到的第一个结果)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UpdateModel的ASP.NET MVC 2问题 - ASP.NET MVC 2 problem with UpdateModel ASP.NET MVC 2 UpdateModel()不更新内存或数据库中的值 - ASP.NET MVC 2 UpdateModel() is not updating values in memory or database asp.NET MVC 2 DataAnnotations UpdateModel <T> 验证 - asp.NET MVC 2 DataAnnotations UpdateModel<T> validation ASP.NET MVC:UpdateModel是一个“昂贵”的操作(由于Reflection)? - ASP.NET MVC: Is UpdateModel an “expensive” operation (due to Reflection)? 如何为ASP.Net MVC伪造UpdateModel? - How do I Fake UpdateModel for ASP.Net MVC? ASP.NET MVC UpdateModel()参数转换失败 - ASP.NET MVC UpdateModel() parameter conversion woes ASP.NET MVC2中嵌套属性的UpdateModel - UpdateModel for Nested Properties in ASP.NET MVC2 ASP.NET MVC UpdateModel 与层次结构 Collections 绑定 - ASP.NET MVC UpdateModel binding with a Hierarchy of Collections ASP.NET MVC 2 - 使用 UpdateModel 和 LINQ to Entities (.NET 3.5) 时“无法更新‘XYZ’类型的模型” - ASP.NET MVC 2 - “The model of type 'XYZ' could not be updated” when using UpdateModel and LINQ to Entities (.NET 3.5) 为什么updateModel()不更新我的模型! 在MVC ASP.NET中用于嵌套的IList <Object> ? - Why updateModel() dont update my model! in MVC ASP.NET for the nested IList<Object>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM