开发者

ForEach with EditorFor

开发者 https://www.devze.com 2022-12-29 12:59 出处:网络
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 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.


You don't need to foreach manually. Just put a file called Message.ascx containing the editor template you've shown inside the ~/Shared/EditorTemplates/ folder and in your view just include it:

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

精彩评论

暂无评论...
验证码 换一张
取 消