在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