MVC3 (ASP.NET) 中何时创建模型对象
尝试了解模型对象何时在 MVC3 中实例化:
我有一个用于编辑“Person”的视图;每个人可以有多个地址。我在人员视图上的网格中显示地址。
这在展示人物时效果很好;我有一个部分视图,它迭代 Person.Addresses
并构建表/网格。
创建新人员时会出现问题:人员对象为 null 并且 Person.Addresses
引用非法。
我确信我在这里遗漏了一些相当基本的东西:因为 MVC 将(自动神奇地)在“保存”时创建新的人员实例;尝试创建自己的对象实例似乎是适得其反的,如果我这样做,则不清楚如何将其连接到表单上的其余条目值。
最后一个复杂之处:地址
列表是可选的;没有地址是完全合法的。
由于关系数据如此常见,因此必须有一个更简单的解决方案来处理这个问题。感谢所有澄清!
Trying to understand when model objects are instantiated in MVC3:
I have a view for editing a "Person"; each person can have multiple addresses. I'm displaying the addresses in a grid on the Person View.
This works great when displaying a person; I have a partial view which iterates through Person.Addresses
and builds the table/grid.
The problem arises when creating a new person: the person object is null and the Person.Addresses
reference is illegal.
I'm certain I'm missing something fairly fundamental here: since MVC will be (auto-magically) creating the new person instance upon "Save"; it would seem counter-production to try and create my own object instance, and if I did, it is unclear how I would connect it to the rest of the entry values on the form.
One last complications: the list of Addresses
is optional; it is perfectly legal not to have an address at all.
As relational data is so common, there has to be a simpler solution for handling this. All clarifications appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题的答案是对象是根据表单中的 POST 数据重新构成的。这是相当基本的,但是 MVC 隐藏了太多正在发生的事情,以至于当您试图了解 (MVC) 方向时很难看到。
项目的顺序是:
注意:
创建页面时:会生成一个表单,其中包含所表示对象的每个部分的字段。 MVC 使用 ID 和其他非显示数据以及验证规则的隐藏字段。
值得注意的是,表单(通常)通过在
_CreateOrEdit.cshtml
页面上列出所有对象属性来创建:或者
通过使用类模板(模板必须与类具有相同的名称)它们代表并位于
Views\Shared\EditorTemplates
文件夹中)。使用模板页面与之前的方法几乎相同:
并且
使用模板方法可以轻松地将(对象)列表添加到表单中。
Person.cshtml
变为:并且
MVC 将根据需要为列表中的每个地址创建尽可能多的表单条目。
POST 的工作原理正好相反;创建模型对象的新实例,调用默认的无参数构造函数,然后 MVC 填充每个字段。通过反转
@Html.EditorFor( model.List )
的序列化过程来填充列表。需要注意的是,您必须确保您的类在构造函数中为列表创建了一个有效的容器,否则 MVC 的列表重新构建将失败:就是这样。幕后发生了很多事情,但都是可以追踪的。
如果您遇到此问题,请注意以下两点:
@Html.HiddenFor(...)
来处理返回服务器的过程中需要“幸存”的所有内容。最后一点:有一篇关于使用 MVC 动态添加/删除列表中的元素的好文章: http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3 这是一篇很好的文章,值得研究——是的,它确实可以与 MVC3 和 Razor 配合使用。
The answer to this question is that the objects are re-constituted from the POST data in the form. This is fairly basic, but MVC hides so much of what is happening that it is hard to see when you are trying to get your (MVC) bearings.
The sequence of items is:
Notes:
When creating the page: a form is generated with fields for each part of the object being represented. MVC uses hidden fields for ID's and other non-displayed data as well as for validation rules.
It is worth noting that forms are created (typically) either by listing all object properties on the
_CreateOrEdit.cshtml
page:and
or by using a template for the class (templates must have the same name as the class they represent and they are located in the
Views\Shared\EditorTemplates
folder).Using template pages is almost identical to the previous method:
and
Using the template method makes it easy to add lists (of object) to the form.
Person.cshtml
becomes:and
MVC will handle creating as many form entries as necessary for each address in the list.
The POST works exactly in reverse; a new instance of the model object is created, calling the default parameter-less constructor, and then MVC fills in each of the fields. Lists are populating by reversing the serialization process of
@Html.EditorFor( model.List )
. It is important to note that you must make certain your class creates a valid container for the list in the constructor otherwise MVC's list re-constitution will fail:That cover's it. There is a lot going on behind the scenes, but it is all track-able.
Two important points if you are having trouble with this:
@Html.HiddenFor(...)
for everything that needs to "survive" the trip back to the server.One last point: there is a good article on dynamically adding / removing elements from a list with MVC: http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3 It's a good article to work through -- and yes, it does work with MVC3 and Razor.
我假设的地址位于表中,而不是实际的输入元素,可能只是 info?
如果是这样,那么它们将不会被实例化,因为没有数据被发送回服务器。
您必须使用回发的元素创建此数据,以便将它们(按名称)映射到其属性。如果模型绑定器可以找到适当命名的发布表单(或查询字符串)元素,则模型绑定器将实例化对象并在回发时将它们传递给控制器。
The addresses I'm assuming are in a table, not actual input elements and likely just <td>info</td>?
If so, then they won't get instantiated as there is no data being posted back to the server.
You must have this data being created with elements that are posted back in order to have them mapped (by name) to their properties. The model binder will instantiate the objects and pass them to your controller upon postback if it can find appropriately named posted form (or querystring) elements.