3

我有一個實體模型,其中包含Message對象的集合,這些對象的類型爲Message,它具有多個屬性,包括內容,MessageID,from和to。ForEach with EditorFor

我已經創建了一個類型爲Message的EditorTemplate,但是,我無法獲取它來顯示Messages集合的內容。

沒有錯誤,但沒有輸出。

請注意,視圖代碼來自父TalkBack類的EditorTemplate。你可以有一個EditorTemplate調用另一個EditorTemplate的子集合嗎?

Talkback和Message類都是由實體框架從現有數據庫生成的。

查看代碼:

 <% foreach (TalkbackEntityTest.Message msg in Model.Messages) 
    { 
      Html.EditorFor(x=> msg, "Message"); 
    } %> 

這是我的模板代碼。這是標準的自動生成的視圖代碼,只需稍作更改。

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TalkbackEntityTest.Message>" %> 

    <%: Html.ValidationSummary(true) %> 

    <fieldset> 
     <legend>Fields</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.MessageID) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.MessageID) %> 
      <%: Html.ValidationMessageFor(model => model.MessageID) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.acad_period) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.acad_period) %> 
      <%: Html.ValidationMessageFor(model => model.acad_period) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.talkback_id) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.talkback_id) %> 
      <%: Html.ValidationMessageFor(model => model.talkback_id) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.From) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.From) %> 
      <%: Html.ValidationMessageFor(model => model.From) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.To) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.To) %> 
      <%: Html.ValidationMessageFor(model => model.To) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.SentDatetime) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.SentDatetime, String.Format("{0:g}", Model.SentDatetime)) %> 
      <%: Html.ValidationMessageFor(model => model.SentDatetime) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.content) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.content) %> 
      <%: Html.ValidationMessageFor(model => model.content) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.MessageTypeID) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.MessageTypeID) %> 
      <%: Html.ValidationMessageFor(model => model.MessageTypeID) %> 
     </div> 

     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 

有消息採集在一定的內容,如果我刪除EditorFor和投入上的Response.Write Message類的內容屬性,我得到的頁面,對3留言內容Field對象,其與預期完全一致。

回答

6

您手動不需要foreach。只要把一個叫Message.ascx含還有你的~/Shared/EditorTemplates/文件夾內,並在您的視圖編輯器模板文件只包含它:

<%: Html.EditorFor(model => model.Messages) %> 
+0

哇,這是得心應手。 MVC比我想象的要聰明:-)。 謝謝Darin。 – hermiod 2010-05-09 21:58:08

+0

我想回到我生命中的最後6個小時:'(非常感謝Darin,你已經保存了一天(或剩下的)! – 2012-11-19 22:55:01