这个问题的答案是,对象是从表单中的POST数据重新构成的。这是相当基本的,但是MVC隐藏了很多事情,当你试图获得你的(MVC)轴承时很难看到。
项目的顺序是:
- 了所有必要的字段创建表;对未显示的键(ID)使用隐藏字段。
- 用户与网页交互;然后按下表单提交按钮。
- 所有字段数据都被发送到控制器页面。
- MVC将数据重新组成类对象。
- 将控制器页面与重新构造的类实例调用为形式参数。
注:
创建页面时:一个形式与用于对象的每个部分的场产生被表示。 MVC为ID和其他非显示数据以及验证规则使用隐藏字段。
值得注意的是,由_CreateOrEdit.cshtml
页面上列出的所有对象属性所创建的形式(通常情况下)或者:
// Edit.cshtml
@model Person
@Html.Partial("_CreateOrEdit", Model)
和
// _CreateOrEdit.cshtml
@model Person
@Html.HiddenFor(model => model.PersonID)
@Html.LabelFor(model => model.first_name, "First Name")
@Html.EditorFor(model => model.first_name)
@Html.LabelFor(model => model.last_name, "Last Name")
@Html.EditorFor(model => model.last_name)
@Html.LabelFor(model => model.favorite_color, "Favorite Color")
@Html.EditorFor(model => model.favorite_color)
//etcetera
,或者通过使用模板类(模板必须与他们代表的班级名称相同,他们位于Views\Shared\EditorTemplates
文件夹中)。
使用模板的页面几乎是相同的以前的方法:
// Edit.cshtml
@model Person
@Html.EditorForModel()
和
// Shared\EditorTemplates\Person.cshtml
@model Person
@Html.HiddenFor(model => model.PersonID)
@Html.LabelFor(model => model.first_name, "First Name")
@Html.EditorFor(model => model.first_name)
@Html.LabelFor(model => model.last_name, "Last Name")
@Html.EditorFor(model => model.last_name)
@Html.LabelFor(model => model.favorite_color, "Favorite Color")
@Html.EditorFor(model => model.favorite_color)
//etcetera
使用模板方法可以很容易地(的对象)列表添加到窗体。 Person.cshtml
变为:
// Shared\EditorTemplates\Person.cshtml
@model Person
@Html.HiddenFor(model => model.PersonID)
@Html.LabelFor(model => model.first_name, "First Name")
@Html.EditorFor(model => model.first_name)
@Html.LabelFor(model => model.last_name, "Last Name")
@Html.EditorFor(model => model.last_name)
@Html.LabelFor(model => model.favorite_color, "Favorite Color")
@Html.EditorFor(model => model.favorite_color)
@EditorFor(model => model.Addresses)
//etcetera
和
// Shared\EditorTemplates\Address.cshtml
@model Address
@Html.HiddenFor(model => model.AddressID)
@Html.LabelFor(model => model.street, "Street")
@Html.EditorFor(model => model.street)
@Html.LabelFor(model => model.city, "City")
@Html.EditorFor(model => model.city)
//etcetera
MVC将处理在必要时创建的列表中的每个地址多份表单条目。
POST正好相反;创建模型对象的新实例,调用默认的无参数构造函数,然后MVC填充每个字段。通过颠倒@Html.EditorFor(model.List)
的序列化过程来填充列表。
public class Person
{
public List<Address> Addresses;
public Person()
{
// You always need to create this List object
Addresses = new List<Address>();
}
...
}
,涵盖的是:需要注意的是,你必须确保你的类创建在构造函数列表中的有效容器否则MVC的名单重新宪法将失败是很重要的。幕后有很多事情要做,但都是可追踪的。
两点很重要,如果你有麻烦与此:
- 请确保您有
@Html.HiddenFor(...)
为需要“生存”之旅回服务器的一切。
- 使用Fiddler或HTTPLiveHeaders(Firefox插件)检查POST数据的内容。这将让您验证正在发回的数据以重新构建新的类实例。我偏爱Fiddler,因为你可以在任何浏览器中使用它(并且它特别好地显示表单数据)。
最后一点:有关于动态添加/删除从MVC列表中元素的好文章:http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3这是一个很好的文章,以合作,通过 - 是的,这确实与MVC3和剃刀工作。
因此,你实际上是一个人,你正在创造一个人吗? – 2011-05-14 22:25:21
正确。编辑人员时,模型对象已经存在。创建人员时,模型对象为空。 – 2011-05-15 16:46:14