在mvc 3 .net FW 4.0项目中,我有一个父子关系相关的数据,我已经建立了我的模型以包含一个列表“孩子”,每个父记录,并显示它下面的例子:Mvc从视图返回空值返回列表<>对象在模型
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<MvcTest.Models.ModelList>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
ShowPropReturn
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>ShowPropReturn</h2>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>ModelList</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.MyProperty1) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.MyProperty1) %>
<%: Html.ValidationMessageFor(model => model.MyProperty1) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.MyProperty2) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.MyProperty2) %>
<%: Html.ValidationMessageFor(model => model.MyProperty2) %>
</div>
<table>
<% foreach (var item in Model.MyProperty3)
{ %>
<tr>
<td>
<%: Html.TextBoxFor(i => item.string1) %>
</td>
<td>
<%: Html.TextBoxFor(i => item.string2) %>
</td>
</tr>
<% } %>
</table>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
我想通了,如果displayfor仅用来显示一个字段的值,它不会调回这是有道理的。但是我在httppost模型中获得了一个空子对象,我需要在同一个视图上编辑这些项目,它在formcollection对象中也是null,任何人都可以帮我解决这个问题吗?
请参阅[达林的回答](https://stackoverflow.com/questions/5895793/mvc-returning-null-value-back-from-view-for-list-object-in-model/5895839#5895839)for解决方案。但为什么你的方法没有工作?传递给'EditorFor'的lambda需要选择页面/控件的* model类型的属性或字段。它不会在范围内传递一些其他东西:'item'永远不会被传递,所以如何在'EditorFor'的实现中使用它? – Richard 2011-05-05 10:05:54