ForEach with EditorFor

时间:2010-05-09 18:38:44

标签: c# entity-framework asp.net-mvc-2 editorfor editortemplates

我有一个Entity模型,其中包含Message对象的集合,Message对象具有多个属性,包括content,MessageID,from和to。

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

没有错误,但没有输出。

请注意,视图代码来自父级Talkback类的EditorTemplate。你有一个EditorTemplate为子集合调用另一个EditorTemplate吗?

Talkback和Message类都是由现有数据库中的Entity框架生成的。

查看代码:

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

Message集合中肯定有内容,如果我删除EditorFor并将response.write放在Message类的content属性上,我会在页面上获得3个Message对象的content字段,这与预期完全一样

1 个答案:

答案 0 :(得分:6)

您无需手动foreach。只需放置一个名为Message.ascx的文件,其中包含您在~/Shared/EditorTemplates/文件夹中显示的编辑器模板,并在您的视图中包含它:

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