2011-05-05 76 views
2

在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,任何人都可以幫我解決這個問題嗎?

+0

請參閱[達林的回答](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

回答

3

取而代之的是foreach循環的請嘗試使用編輯模板:

<table> 
    <%= Html.EditorFor(x => x.MyProperty3) 
</table> 

和相應的編輯器模板中(~/Views/Shared/EditorTemplates/SomeModelType.ascx):

<%@ Control Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.SomeModelType>" 
%> 
<tr> 
    <td> 
     <%= Html.TextBoxFor(x => x.string1) %> 
    </td> 
    <td> 
     <%= Html.TextBoxFor(x => x.string2) %> 
    </td> 
</tr> 

現在,這將產生正確的輸入字段名,這樣,當您提交的表單值將被正確綁定。

備註:在這種情況下,我認爲MyProperty3在你的主視圖模型是這樣定義(的SomeModelType集合):

public IEnumerable<SomeModelType> MyProperty3 { get; set; } 
+0

非常感謝您的快速回復,完美工作! – Erika 2011-05-05 12:21:50

1

埃裏卡 - 我能夠通過使用循環

,使這項工作
for (int i = 0; i < Model.MyList.Count(); i++) { 
    @Html.TextboxFor(m => m.MyList[i].someproperty) 
} 

但是,我更喜歡達林的解決方案。

而且我發現,綁定,如果你設置一個輔助的名稱屬性將無法正常工作

1

Darin的答案是正確的,但有一個警告:你需要有一個字段的形式爲每「套」字段,否則它不匹配,並且生成的模型列表爲空。

考慮這種情況下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; } 

...是整個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 

    <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> 

,那麼你必須在形式和AccountModel對象,AccountModel的列表之間的完全匹配將包含複選框所有成員(和更新的值)。

+0

很好的解釋。以這種方式添加所有字段幫助我解決了這個問題。 – Jeroen 2013-10-22 13:34:11