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