什么是ASP.NET MVC的UpdateModel正确行为? [英] What is correct behaviour of UpdateModel in ASP.NET MVC?

查看:171
本文介绍了什么是ASP.NET MVC的UpdateModel正确行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣知道你们觉得应该在ASP.NET MVC的的UpdateModel 方法上被视为正确的行为。

我在这里问其原因或许是,如果这个功能是设计有人能澄清,为什么它是事情是这样的,也许还有办法不同调用它来实现所需的功能,我会想象会路90%的民间希望这工作吗?

在本质上,我的抱怨在于绑定过程的行为在的UpdateModel

假如您希望通过对这些表单上的数据字段反映你的数据库模型一个简单的保存操作方法更新形式,最初去拯救的要求,我们可能会从数据库中已有的模型,然后更新这其中发生了变化相关领域,由的UpdateModel <通过的FormCollection 发送,然后更新/ code>我们现有的模型。此功能,但它出现任何到此DB-填充对象的现有属性正在被复位;而我的意思是,被设置为null或初始化默认值就好像它是一个全新的对象,除了明显的匹配,其中的FormCollection

这是一个问题,因为它存在的对象,但表单上不一定存在,如面向字段的任何子集合或对象,日期或任何非UI的任何现有属性为空,留下一个半不能被保存到数据库,因为所有丢失的数据可能包括ID的现在设置为0。一叠人口,或多或少无法使用对象

我相信这是不可取的行为,的UpdateModel 应该只更新它找到的FormCollection 。这意味着所有现有的属性将是不变的,但您的更新将被设置。然而,从什么迄今推导,显然这是不是这样的 - 它似乎它也会根据形式的对象的全新副本的更新属性,然后返回新的对象

最后,要正确看待它,如何的负担,这是真的围绕它保存一个半复杂的形式,并保留所有现有的对象数据是手动结婚了每个与相应的表单属性属性,以绝对保证只存在于形式的属性会被更新。

我猜,


  1. 这些谁同意这是由设计,是我结婚的最好方式形式的方法呢?

  2. 或者,你是怎样在这解决了这个?

请随时提供这个家伙你的想法,谢谢。

下面是有人从这个问题的痛苦的另一个实例:结果
<一href=\"http://stackoverflow.com/questions/1207991/calling-updatemodel-with-a-collection-of-complex-data-types-reset-all-non-bound-v\">http://stackoverflow.com/questions/1207991/calling-updatemodel-with-a-collection-of-complex-data-types-reset-all-non-bound-v


解决方案

您正在使用的UpdateModel()遇到的行为听起来像你列表绑定,在这种情况下的UpdateModel()将吹走列表的内容,重新填充它。见<一href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\">Hanselman's博客查找有关此的讨论。如果您要更新单个对象,的UpdateModel()将更新该单个对象,离开没有相应的表单值,是性能。

许多问题都归结到的UpdateModel()真的是为了重新填充视图模型 - 而不是域模型 - 基于表单输入。 (我微跌说,视图模型只是一个控制器和视图之间的合同,而你的域模型可能是LINQ2SQL或EF模型对象简化了的东西。)所有的MVC教程和演示展示的UpdateModel()是用来对付数据库对象,这我觉得是不幸的,因为它为模型结合预期的目的有点误导。罗伯特的职位是更指示的UpdateModel()。

的实际意图

I am interested to know what you guys feel should be deemed "correct behaviour" in terms of the UpdateModel method in ASP.NET MVC.

The reason I ask here is perhaps if this functionality is "by design" somebody could clarify as to why it is the way it is, and perhaps a way to call it differently to achieve desired functionality, which I would imagine would be the way 90% of folk would want this to work?

In essence, my gripe lies with the behaviour of the binding process within UpdateModel.

Supposing you wish to update a form via a simple Save action method for which the data fields on the form reflects a model in your database, initially to go about saving the request, we might get the existing model from the DB, and then update relevant fields which which were changed, sent via FormCollection and then updated by UpdateModel to our existing model. This functions, however it appears any of the existing properties on this DB-populated object are being "reset"; and by that I mean, are being set to null or initialisation defaults just as if it was a brand new object, except for obviously those which match those in the FormCollection.

This is a problem because any existing properties which exist on the object, but not necessarily exist on the form, such as any child collections or objects, dates or any non-UI facing fields are empty, leaving you with a half-populated, more or less unusable object which can't be saved to the DB because of all the missing data including probably a stack of ID's now set to 0.

I believe this is not desirable behaviour, and UpdateModel should only update properties where it finds a property match in FormCollection. This would mean all your existing properties would be untouched, but your updates would be set. However, from what has been deduced so far, obviously this is not the case - it appears it instantiates a brand new copy of the object updates the properties from the form, then returns the new object.

Finally, to put it in perspective of how much of a burden this is, the only way really around it to save a half-complex form and keep all your existing object data is to manually marry up each property with the corresponding form property to absolutely guarantee only properties that exist in the form are being updated.

I guess,

  1. Those who agree this is by design, is my approach of form marrying the best way?
  2. Or, how have you tackled this in this?

Please feel free to offer your thoughts on this guys, Thanks.

Here is another instance of somebody suffering from this problem:
http://stackoverflow.com/questions/1207991/calling-updatemodel-with-a-collection-of-complex-data-types-reset-all-non-bound-v

解决方案

The behavior you're experiencing with UpdateModel() sounds like you're list binding, in which case UpdateModel() will blow away the contents of the list and repopulate it. See Hanselman's blog for a discussion on this. If you're updating a single object, UpdateModel() will update that individual object, leaving properties that don't have a corresponding form value as-is.

Many of these problems boil down to that UpdateModel() is really meant to repopulate view models - not domain models - based on form input. (I'm slightly simplifying things by saying that a view model is just a contract between a controller and the view, while your domain model might be a LINQ2SQL or EF model object.) All of the MVC tutorials and demos show UpdateModel() being used against database objects, which I feel is unfortunate since it's somewhat misleading as to the intended purpose of model binding. Robert's post is more indicative of the actual intent of UpdateModel().

这篇关于什么是ASP.NET MVC的UpdateModel正确行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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