【发布时间】:2011-05-05 09:56:18
【问题描述】:
在 mvc 3 .Net FW 4.0 项目中,我有一个与父子关系相关的数据,我建立了我的模型以包含每个父记录的“子”列表,并将其显示为下面的例子:
<%@ 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 对象中也是空的,有人可以帮我吗?
【问题讨论】:
-
请参阅Darin's answer 了解解决方案。但为什么你的方法不起作用?传递给
EditorFor的 lambda 需要选择页面/控件的 模型类型 的属性或字段。它不会在范围内传递一些其他东西:item永远不会传递,那么它如何在EditorFor的实现中使用?
标签: model-view-controller asp.net-mvc-3