3

콘텐츠, MessageID, 보낸 사람 및받는 사람을 포함하여 여러 가지 속성을 가진 Message 형식의 Message 개체 컬렉션이 포함 된 Entity 모델이 있습니다.ForEach with EditorFor

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> 

내가 EditorFor를 제거하고 메시지 클래스의 내용 특성에 Response.Write를 넣어 경우, 나는 3 메시지의 내용 필드는 페이지에 개체를 얻을, 같은 메시지 컬렉션에서 확실히 내용이있는 정확히 예상대로입니다.

답변

6

수동으로 foreach을 필요로하지 않습니다. 그냥 단지를 포함하면 ~/Shared/EditorTemplates/ 폴더 안에 당신의보기에 표시 한 편집기 템플릿을 포함 Message.ascx라는 파일을 넣어 :

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

와우,이 편리합니다. MVC는 내가 기대했던 것보다 더 똑똑하다. 감사합니다. – hermiod

+0

나는 내 인생의 마지막 6 시간을 되찾고 싶다. '(대단히 감사합니다. 당신은 그 날 (또는 그게 남아있는 것)을 다시 구했습니다! –