Model view controller Mvc从视图返回列表的空值<&燃气轮机;模型中的对象
在mvc 3.Net FW 4.0项目中,我有一个与父子关系相关的数据,我建立了我的模型,其中包含一个包含每个父记录的“子项”列表,并如以下示例所示显示它:Model view controller Mvc从视图返回列表的空值<&燃气轮机;模型中的对象,model-view-controller,asp.net-mvc-3,Model View Controller,Asp.net Mvc 3,在mvc 3.Net FW 4.0项目中,我有一个与父子关系相关的数据,我建立了我的模型,其中包含一个包含每个父记录的“子项”列表,并如以下示例所示显示它: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<MvcTest.Models.ModelList>" %> <asp:Content
<%@ 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>
ShowPropReturn
ShowPropReturn
模型列表
型号.MyProperty1)%>
型号.MyProperty1)%>
型号.MyProperty1)%>
型号.MyProperty2)%>
型号.MyProperty2)%>
型号.MyProperty2)%>
项目1)%>
项目2)%>
我发现,如果displayfor只用于显示字段值,那么它不会被发回,这是有意义的。但是我在httppost的模型中得到了一个空的子列表对象,我需要在同一个视图中编辑这些项目,formcollection对象中也是空的,有人能帮我吗?代替
foreach
循环尝试使用编辑器模板:
<table>
<%= Html.EditorFor(x => x.MyProperty3)
</table>
现在,这将生成正确的输入字段名,以便在提交表单时正确绑定表单值
备注:在这种情况下,我假设主视图模型上的MyProperty3
定义如下(一组SomeModelType
):
public IEnumerable MyProperty3{get;set;}
埃里卡-我能够通过使用for循环来实现这一点
for (int i = 0; i < Model.MyList.Count(); i++) {
@Html.TextboxFor(m => m.MyList[i].someproperty)
}
for(int i=0;im.MyList[i].someproperty)
}
然而,我更喜欢Darin的解决方案
此外,我还发现,如果设置助手的Name属性,绑定将不起作用。Darin的回答是正确的,但有一点需要注意:您需要在表单中为模型中的每个“set”字段设置一个字段,否则它将不匹配,并且生成的模型列表为空 考虑以下AccountModel列表的情况:
public class AccountModel
{
public AccountType Type { get; set; }
public bool Selected { get; set; }
public string Name { get { return Util.GetAccountTypeName(Type); } }
}
父视图只需要包含
@Html.EditorFor(model => model.Accounts)
。。。在哪里
List<AccountModel> Accounts { get; set; }
列出帐户{get;set;}
。。。是整个ViewModel的一部分
现在,如果您像这样创建编辑器模板
@model WebLinx.Models.AccountModel
<div class="informationRow">
@Html.CheckBoxFor(x => x.Selected)
<div class="formLabel">
@Html.Label(Model.Name)
</div>
</div>
@model WebLinx.Models.AccountModel
@Html.CheckBoxFor(x=>x.Selected)
@Html.Label(Model.Name)
。。。然后,在提交时,您将在父模型中返回一个空对象,即使在显示“带有复选框的帐户列表”视图时显示为完整
但如果您包括一个额外的行:
@model WebLinx.Models.AccountModel
<div class="informationRow">
@Html.CheckBoxFor(x => x.Selected)
@Html.HiddenFor( x=> x.Type) @* extra line with hidden field *@
<div class="formLabel">
@Html.Label(Model.Name)
</div>
</div>
@model WebLinx.Models.AccountModel
@Html.CheckBoxFor(x=>x.Selected)
@Html.HiddenFor(x=>x.Type)@*带隐藏字段的额外行*@
@Html.Label(Model.Name)
然后表单和AccountModel对象之间就完全匹配了,AccountModel列表将包含所有成员(以及复选框的更新值)。很好的解释。以这种方式添加所有字段帮助我解决了问题。有关解决方案,请参阅。但是为什么你的方法不起作用呢?传递给
EditorFor
的lambda需要选择页面/控件模型类型的属性或字段。它不会在作用域中传递其他内容:项
永远不会传递,因此如何在EditorFor
的实现中使用它?
@model WebLinx.Models.AccountModel
<div class="informationRow">
@Html.CheckBoxFor(x => x.Selected)
<div class="formLabel">
@Html.Label(Model.Name)
</div>
</div>
@model WebLinx.Models.AccountModel
<div class="informationRow">
@Html.CheckBoxFor(x => x.Selected)
@Html.HiddenFor( x=> x.Type) @* extra line with hidden field *@
<div class="formLabel">
@Html.Label(Model.Name)
</div>
</div>