ASP.Net MVC DefaultModelBinder 未绑定 POST 上的属性
我面临着一个非常奇怪的问题,让我抽烟。
我有一个相当简单的场景,其中我有一个强类型视图,该视图是从 GET 上的控制器正确填充的,但是当它将表单发布到控制器时,Reqeust 充满了所有正确的值和正确的键名称。默认模型绑定器可以正确填充我的模型对象之一,并且 DMB 创建正确的对象,但它永远不会填充任何属性,它们都处于默认状态。
这之前是有效的,我能想到的唯一改变是我尝试了一个自定义模型绑定器(然后将其删除;仔细检查以确保我不再使用它),并且我重构了模型以具有一个基类,其中包含一些的道具。
有什么想法吗?
I'm facing a really strange problem that has me smoked.
I have a fairly simple scenario where I have a strongly typed view that is correctly populated from the controller on the GET, but then when it POSTS the form to the controller, the Reqeust is full of all the right values and right key names for the default model binder to correctly populate one of my model objects, and the DMB creates the correct opject, but it never populates any of the properties, they're all in their default state.
This was working before, the only changes I can think of were that I tried a custom modelbinder (then removed it; double checked to make sure I'm not still using that), and I refactored the model to have a base class with some of the props.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个非常相似的场景 - DefaultModelBinder - 本质上 - 不绑定到你的模型,如果你给绑定的模型对象提供与其属性之一相同的名称:
Model
View
<强>控制器
(使用 ASP.NET MVC 3)
A very similar scenario - that the DefaultModelBinder is - essentially - not binding to your model, arrise if you would give your bound model object the same name as one of its properties:
Model
View
Controller
(Using ASP.NET MVC 3)
知道了。 该模型的重构方式自然会影响 mdoel 绑定器填充模型的能力。
Got it. The model had been refactored in a way which naturally affected the ability of the mdoel binder to populate it.
输入参数的名称不必等于对象的某些属性名称。 请记住,所有数据均以名称数组形式出现 -> 值和默认绑定使用名称来使关系发挥作用。
The name of your input param do not have to be equal to some property name of the object. Remember that all data coming as an array of name -> value and the default binding use the names for make the relation work.
我通过将两个属性从类的顶部移动到更下面来出现这种行为。 我仍然不明白为什么这会阻止第三个属性的绑定工作(所以这不是一个解决方案,而是“注意”),但我多次重复更改,每次绑定都从工作到不工作。
我还发现,进行此更改后,我有时必须“清理”解决方案才能使绑定再次开始工作。
I had this behaviour arise by moving two properties from the top of the class to further down. I still can't work out why this stopped the binding of a third property from working (so this isn't a solution so much as a 'watch out for') but I repeated the change multiple times and each time the binding went from working to not working.
I also found that after making this change I sometimes had to 'Clean' the solution for the binding to start working again.