C# ForEach与EditorFor

C# ForEach与EditorFor,c#,entity-framework,asp.net-mvc-2,editorfor,editortemplates,C#,Entity Framework,Asp.net Mvc 2,Editorfor,Editortemplates,我得到了一个实体模型,其中包含消息对象的集合,消息对象的类型为Message,它有几个属性,包括content、MessageID、from和to 我已经为Message类型创建了EditorTemplate,但是,我无法让它显示Messages集合的内容 没有错误,但没有输出任何内容 请注意,视图代码来自父对讲类的EditorTemplate。能否让EditorTemplate为子集合调用另一个EditorTemplate 对讲和消息类都是由实体框架从现有数据库生成的 查看代码:

我得到了一个实体模型,其中包含消息对象的集合,消息对象的类型为Message,它有几个属性,包括content、MessageID、from和to

我已经为Message类型创建了EditorTemplate,但是,我无法让它显示Messages集合的内容

没有错误,但没有输出任何内容

请注意,视图代码来自父对讲类的EditorTemplate。能否让EditorTemplate为子集合调用另一个EditorTemplate

对讲和消息类都是由实体框架从现有数据库生成的

查看代码:

        <% foreach (TalkbackEntityTest.Message msg in Model.Messages)
    { 
           Html.EditorFor(x=> msg, "Message"); 
    } %>
msg,“消息”);
} %>
这是我的模板代码。它是标准的自动生成的视图代码,只做了一些小的更改

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

领域
model.MessageID)%%>
model.MessageID)%%>
model.MessageID)%%>
型号:acad_期间)%>
型号:acad_期间)%>
型号:acad_期间)%>
型号.对讲_id)%>
型号.对讲_id)%>
型号.对讲_id)%>
model.From)%%>
model.From)%%>
model.From)%%>
型号(至)%>
型号(至)%>
型号(至)%>
model.SentDatetime)%%>
model.SentDatetime,String.Format(“{0:g}”,model.SentDatetime))%>
model.SentDatetime)%%>
型号.内容)%>
型号.内容)%>
型号.内容)%>
model.MessageTypeID)%%>
model.MessageTypeID)%%>
model.MessageTypeID)%%>


消息集合中肯定有内容,因为如果我删除EditorFor并进行响应。在Message类的content属性上写入,我将获得页面上3个消息对象的content字段,这与预期完全一致。

您不需要手动执行
foreach
。只需将一个名为
Message.ascx
的文件放入
~/Shared/EditorTemplates/
文件夹中,并在视图中包含以下编辑器模板:

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

哇,这很方便。MVC比我想象的更聪明:-)。谢谢你,达林。我想要回我生命中的最后6个小时:'(非常感谢你,达林。你又一次拯救了这一天(或者说是剩下的那一天)!