2012-09-20 2 views
0

텍스트 파일이 있고 사용자가 파일을 업로드하면 컨트롤러 작업 메서드는 상태 시스템을 사용하여 해당 파일을 구문 분석하고 일반 목록을 사용하여 일부 값을 저장합니다 . 이 뷰를 IEnumerable 형태로 다시 전달합니다. 내 기본보기 내에서이 ienumerable 목록을 기반으로 항목을 반복하고 레이블과 텍스트 영역을 표시하기위한 partail보기를 렌더링합니다. 사용자는 텍스트 영역에 입력 내용을 추가 할 수 있습니다. 사용자가 저장 단추를 누르면 렌더링 된 부분 뷰에서이 iumumrable 목록이 null입니다. 그래서 어떤 해결책을 조언하십시오. 다음과 같이 여기 보기 모델 렌더링 된 모델 내에서 IEnumerable 목록을 검색하는 방법

내 기본보기

@model RunLog.Domain.Entities.RunLogEntry 
@{ 
    ViewBag.Title = "Create"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 

} 

    @using (Html.BeginForm("Create", "RunLogEntry", FormMethod.Post, new { enctype = "multipart/form-data" })) 
    { 

<div id="inputTestExceptions" style="display: none;"> 
     <table class="grid" style="width: 450px; margin: 3px 3px 3px 3px;"> 
      <thead> 
       <tr> 
        <th> 
         Exception String 
        </th> 
        <th> 
         Comment 
        </th> 
       </tr>    </thead> 

      <tbody> 
       @if (Model.TestExceptions != null) 
       { 
        foreach (var p in Model.TestExceptions) 
        { 
         Html.RenderPartial("RunLogTestExceptionSummary", p); 

        } 
       } 
      </tbody> 
     </table> 

    </div> 
    } 

부분이다 :

@model RunLog.Domain.Entities.RunLogEntryTestExceptionDisplay 
<tr> 
    <td> 
    @[email protected] 
     </td> 
     <td>@Html.TextAreaFor(Model.Comment, new { style = "width: 200px; height: 80px;" }) 
    </td> 
</tr> 

컨트롤러 액션을

[HttpPost] 
    public ActionResult Create(RunLogEntry runLogEntry, String ServiceRequest, string Hour, string Minute, string AMPM, 
           string submit, IEnumerable<HttpPostedFileBase> file, String AssayPerformanceIssues1, IEnumerable<RunLogEntryTestExceptionDisplay> models) 
    { 

}

문제는 사기꾼 테스트 예외입니다 예외 문자열을 무시하고 주석이 null로 되돌아갑니다.

public class RunLogEntry 
{ 
    SOME OTHER FIELDS 

    [NotMapped] 
    public IEnumerable<RunLogEntryTestExceptionDisplay> TestExceptions { get; set; } 
} 



public class RunLogEntryTestExceptionDisplay 
{ 
    public string TestException { get; set; } 
    public string Comment { get; set; } 
} 



@using (Html.BeginForm("Create", "RunLogEntry", FormMethod.Post, new { enctype = "multipart/form-data" })) 
    { 
     if (Model.TestExceptions != null) 
      { 
       if (Model.TestExceptions.Count() > 0) 
       { 
     <div class="bodyContent"> 
      <span class="leftContent"> 
       @Html.Label("Test Exceptions") 
      </span><span class="rightContent"><span id="TestExceptionChildDialogLink" class="treeViewLink"> 
       Click here to View Test Exceptions</span> 
       <br /> 
       <span id="TestExceptionDisplay"></span> 
       @Html.HiddenFor(model => model.TestExceptions) 
       @*<input id="ExceptionString" type="hidden" value="@Model.ExceptionString" />*@ 
      </span> 
     </div> 
       } 
      } 


    <div id="inputTestExceptions" style="display: none;"> 
     <table class="grid" style="width: 450px; margin: 3px 3px 3px 3px;"> 
      <thead> 
       <tr> 
        <th> 
         Exception String 
        </th> 
        <th> 
         Comment 
        </th> 
       </tr> 
      </thead> 
      @if (Model.TestExceptions != null) 
      { 
       var index = 0; 
       foreach (var p in Model.TestExceptions) 
       { 
       <tr> 
        <td>@p.TestException 
         <input type="hidden" name="RunLogEntry.TestExceptions[@index].ExceptionString" value="@p.TestException" /> 
        </td> 
        <td> 
         <textarea name="RunLogEntry.TestExceptions[@index].Comment" style ="width: 200px; height: 80px;">@p.Comment</textarea> 
         <input type="hidden" name="RunLogEntry.TestExceptions[@index].Comment" value="@p.Comment" /> 
        </td> 
        @* Html.RenderPartial("RunLogTestExceptionSummary", p);*@ 
       </tr> 
                           index++; 
       } 

      } 
     </table> 
    </div> 
} 

답변

0

UPDATE 당신은 배열 스타일의 폼 요소의 이름을 지정해야, 컬렉션을 게시합니다. 또한 html 요소 이름/id에 배열 인덱스를 설정할 수 있도록 카운터를 유지해야합니다.

@{var index = 0;} 
@foreach (var p in Model.TestExceptions) { 
    <tr> 
    <td> 
     @[email protected] 
    </td> 
     <td> 
      <textarea name="@Html.FieldNameFor(m => m.TestExceptions[@index].Comment" id="@Html.FieldIdFor(m => m.TestExceptions[@index].Comment" style ="width: 200px; height: 80px;">@p.Comment</textarea> 
      <input type="hidden" name="@Html.FieldNameFor(m=> m.TestExceptions[@index].ExceptionString" id="@Html.FieldIdFor(m=> m.TestExceptions[@index].ExceptionString" value="@p.ExceptionString" /> 
     </td> 
    </tr> 
index++; 
} 

나는 당신의 모델이 어떻게 생겼는지 모르겠지만, 당신은 예외 문자열을 언급, 그래서 바인딩 모델을 선택할 수 있도록 나는 "ExceptionString"속성 값을 저장하는 숨겨진 양식 요소를 사용 POST 할 때이 값.

RunLogEntry 클래스가 TestExceptions 컬렉션 이외의 다른 속성을 가지고있는 경우 RunLogEntry 속성에 매핑되는 양식 요소를 포함하여 POST에서 올바르게 바인딩되도록 할 수 있습니다. 당신이 RunLogEntry.Foo 속성이있는 경우 예를 들어, 위의 루프 외부에서이 곳을 넣을 것 :이 모델 RunLogEntry 공공는 IEnumerable TestExceptions {얻을 내 호텔 중 하나입니다

<input type="hidden" name="RunLogEntry.Foo" value="@Model.Foo" /> 
+0

; 세트; } – user1475788

+0

그러면이 @model RunLog.Domain.Entities.RunLogEntryTestExceptionDisplay에는 두 가지 속성, 즉 테스트 예외 문자열과 주석 문자열 – user1475788

+0

이 주보기에 있습니다. im은 테스트 예외 표시를 루프 내에서 부분적으로 렌더링하므로 잘 렌더링됩니다. 게시물을 볼 수 없습니다. – user1475788