ForEach with EditorFor

Posted by hermiod on Stack Overflow See other posts from Stack Overflow or by hermiod
Published on 2010-05-09T18:38:44Z Indexed on 2010/05/09 18:48 UTC
Read the original article Hit count: 349

I have got an Entity model which contains a collection of Message objects which are of the type Message which has several properties, including content, MessageID, from, and to.

I have created an EditorTemplate for type Message, however, I cannot get it to display the contents of the Messages collection.

There are no errors, but nothing is output.

Please note that the view code is from an EditorTemplate for the parent Talkback class. Can you have an EditorTemplate calling another EditorTemplate for a child collection?

Both the Talkback and Message class are generated by Entity framework from an existing database.

View code:

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

This is my template code. It is the standard auto-generated view code with some minor changes.

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

There is definitely content in the Message collection as, if I remove EditorFor and put in response.write on the content property of the Message class, I get the content field for 3 Message objects on the page, which is exactly as expected.

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about c#