2017-02-02 8 views
1

버튼을 클릭 할 때마다 양식에 추가되는 양식 내 부분보기가 있습니다. 일련의 응답이있는 텍스트 영역입니다. 모델을 뷰에서 컨트롤러로 전달하고 목록 모델에 추가 한 다음 다시 볼 수 있습니까?보기에서 컨트롤러로 모델을 전달하는 방법, 목록에 항목 추가,보기로 돌아 가기

ReviewFormViewModel을 전달합니다. ListAdhoc을 부분 컨트롤러에 전달하고 항목을 추가 한 다음 다시보기로 전달하려고합니다. ReviewFormViewModel를 사용하는 부분도

public PartialViewResult Adhoc() 
    { 
     //pass model object on button click and add each item to the model everytime 
     var AdhocObj = new AdhocViewModel(); 

     AdhocObj.ListAdhocOptions = new List<AdhocOptionsVM>(); 
     var query = db.dbQuestionOptions.Where(qo => qo.ActiveFl == "Y").OrderByDescending(qo => qo.Rating).ToList(); 

     foreach (var item in query) 
     { 
      var AdhocAnsOptionsVMObj = new AdhocOptionsVM(); 
      AdhocAnsOptionsVMObj.AnswerId = item.AnswerId; 
      AdhocAnsOptionsVMObj.RatingName = item.RatingName; 
      AdhocAnsOptionsVMObj.Rating = item.Rating; 
      AdhocAnsOptionsVMObj.ActiveFl = item.ActiveFl; 

      AdhocObj.ListAdhocOptions.Add(AdhocAnsOptionsVMObj); 
     } 


     return PartialView("Adhoc", AdhocObj); 
} 

부분적인 뷰

public class ReviewFormViewModel 
{ 
    ...// other fields 
    public List<AdhocViewModel> ListAdhoc { get; set; } 
} 

public class AdhocViewModel 
{ 
    public int? ReviewId { get; set; } 
    public String AdhocQuestion { get; set; } //free form 
    public int? SelectedAnswer { get; set; } // for binding int? for optional 
    public String Comments { get; set; } 
    public List<AdhocOptionsVM> ListAdhocOptions { get; set; } 
} 

public class AdhocOptionsVM 
{ 
    public int AnswerId { get; set; } 
    public String RatingName { get; set; } 
    public Decimal Rating { get; set; } 
    public String ActiveFl { get; set; } 

} 

제어기 aswell :

<div class="adhoc"> 
@using (Html.BeginCollectionItem("adhoc")) 
{ 
    <div class="panel panel-success"> 
     <div class="panel-heading"> 
      @Html.HiddenFor(m => m.ReviewId) 
      @Html.HiddenFor(m => m.AdhocId) 

      @Html.TextAreaFor(m => m.AdhocQuestion, htmlAttributes: new { @style = "width:650px", @placeholder = "Enter Adhoc Question here" })<br /> 
     </div> 
     <div class="panel-body"> 
      @foreach (var optAnswer in Model.ListAdhocOptions) 
      { 
       <div class="radio"> 
        <responselabel>@Html.RadioButtonFor(m => m.SelectedAnswer, optAnswer.AnswerId, new { id = optAnswer.AnswerId }) @optAnswer.RatingName</responselabel><br /> 
       </div> 
      } 
      <div>@Html.ValidationMessageFor(m => m.SelectedAnswer)</div><br /> 

      @Html.TextAreaFor(m => m.Comments, htmlAttributes: new { @style = "width:650px", @placeholder = "Comments" })<br /><br /> 
     </div> 
     <button type="button" class="delete">Delete</button> 
    </div> 

} 

메인 뷰

@model CustomerFeedback.Areas.ProjectManagers.Models.ReviewFormViewModel 

@{ 
    ViewBag.Title = "CreateFormsIndex"; 
} 

<h4 align="center">Project Review Form</h4> 

<div class="container-fluid"> 
    <div class="row"> 
     <div class="col-md-12"> 
      <div class="text-center"> 
       <h4> 
        @Html.DisplayName(Model.ProjectId) @Html.DisplayName(Model.ProjectName) 
       </h4> 
       <h4> 
        PM: @Html.DisplayName(Model.FullName) 
       </h4> 
      </div> 
     </div> 
    </div> 
</div> 

<div class="container"> 
    <br /> 
    <div class="panel-group"> 
     @using (Html.BeginForm()) 
     { 

      @Html.AntiForgeryToken() 
      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
      @Html.HiddenFor(m => m.ProjectId) 
      @Html.HiddenFor(m => m.AccountId) 
      @Html.HiddenFor(m => m.ReviewDate) 

      <div class="panel panel-default"> 
       <div class="panel-body"> 
        <div class="panel-group"> 
         <div class="panel-heading"> 
          <h4 class="panel-title"> 
           Required Questions 
          </h4> 
         </div> 
         @for (int i = 0; i < Model.ListReqQuestions.Count; i++) 
         { 
          <div class="panel panel-success"> 
           <div class="panel-heading"> 
            @Html.HiddenFor(m => m.ListReqQuestions[i].QuestionId) 
            @Html.DisplayFor(m => m.ListReqQuestions[i].QuestionText) 
           </div> 
           <div class="panel-body"> 
            @foreach (var optAnswer in Model.ListReqQuestions[i].ListQuestionOptions) 
            { 
             <div class="radio"> 
              <responselabel>@Html.RadioButtonFor(m => m.ListReqQuestions[i].SelectedAnswer, optAnswer.AnswerId, new { id = optAnswer.AnswerId }) @optAnswer.RatingName</responselabel><br /> 
             </div> 
            } 
            <div>@Html.ValidationMessageFor(m => m.ListReqQuestions[i].SelectedAnswer)</div><br /> 

            @Html.TextAreaFor(m => m.ListReqQuestions[i].Comments, htmlAttributes: new { @style = "width:650px", @placeholder = "Comments" })<br /><br /> 
           </div> 
          </div> 
         } 

         <div class="panel-heading"> 
          <h4 class="panel-title"> 
           Optional Questions 
          </h4> 
         </div> 
         @for (int i = 0; i < Model.ListOpQuestions.Count; i++) 
         { 
          <div class="panel panel-success"> 
           <div class="panel-heading"> 
            @Html.HiddenFor(m => m.ListOpQuestions[i].QuestionId) 
            @Html.DisplayFor(m => m.ListOpQuestions[i].QuestionText) 
           </div> 
           <div class="panel-body"> 
            @foreach (var optAnswer in Model.ListOpQuestions[i].ListQuestionOptions) 
            { 
             <div class="radio"> 
              <responselabel>@Html.RadioButtonFor(m => m.ListOpQuestions[i].SelectedAnswer, optAnswer.AnswerId, new { id = optAnswer.AnswerId }) @optAnswer.RatingName</responselabel><br /> 
             </div> 
            } 
            <div>@Html.ValidationMessageFor(m => m.ListOpQuestions[i].SelectedAnswer)</div><br /> 

            @Html.TextAreaFor(m => m.ListOpQuestions[i].Comments, htmlAttributes: new { @style = "width:650px", @placeholder = "Comments" })<br /><br /> 
           </div> 
          </div> 
         } 
         @*on click (new adhoc question) add a new freeform question with list of answers*@ 
         <div class="panel panel-success" id="adhoc"> 
          @* renders partial adhoc view *@ 
         </div> 
         <br /> 
         <div class="center"> 
          <input type="button" value="New Adhoc Question" class="btnAdhoc btn-success" /> 
         </div> 
         <br /> 
         <div class="center"> 
          <input type="submit" value="Save" name="Command" class="btn btn-success" /> 
          <input type="submit" value="Submit" name="Command" class="btn btn-success" /> 
          <input type="submit" value="Cancel" name="Command" class="btn btn-success" /> 
          <input type="submit" value="Attach" name="Command" class="btn btn-success" /> 
         </div> 

        </div> 
       </div> 
      </div> 
     } 
    </div> 
</div> 

<script> 

    $(function() { 

     $('.btnAdhoc').click(function (event) { 
      event.preventDefault(); 

      $.ajax({ 
       url: '/ProjectManagers/Forms/Adhoc', 
       //data: JSON.stringify(model), 
       type: 'get', 
       success: function (result) { 
        $('#adhoc').append(result); 
       } 
      }); 
     }); 
    }) 
</script> 

업데이트 : AdhocViewModel을 추가했습니다.

Ive는 이러한 속성에 대한보기 모델을 추가했습니다. 저는 일련의 질문과 답변을 담은 양식을 가지고 있습니다. 그것들은 데이터베이스에 있습니다. 나는 버튼을 가지고 있으며, 클릭하면 부분보기가 생성되고 양식에 추가됩니다 (많은 수 있음). 부분보기는 텍스트 영역 (입력 된 모든 질문에 대해), 응답 집합 (데이터베이스에서) 및 설명 상자로 구성됩니다. 게시물 (제출)에서 어떻게 처리해야할지 모르겠습니다. 내 시도는보기에서 부분 컨트롤러로 모델을 전달하고 항목을 추가 한 다음 처리를 위해보기로 다시 전달하는 것입니다. 모델 데이터를 전달할 때 어떤 성공도 없습니다.

UPDATE 2 BeginCollectionItem 도우미를 사용하여 코드를 업데이트했습니다. 추가 된 기본보기

+0

여기에서하려는 것을 이해하기 어렵습니다. 아니, 부분적으로'ListAdhoc'의 속성과 관련된 것을 보여 줬습니까? –

+0

하지만 부분적으로 코드를 기반으로하지 않는 부분 인'ReviewFormViewModel'을 사용한다고 주장했습니다 -'@model AdhocViewModel'이어야합니다 –

+0

그리고 그 부분 'AdhocViewModel'에 대해서'ReviewFormViewModel'은'AdhocViewModel'의 콜렉션을 포함하고 있으며 콜렉션에 대한 컨트롤을 생성하기 위해 인덱서를 가질 필요가 있고 부분적으로는 그렇게하지 않을 것이기 때문에 말이되지 않습니다. –

답변

1

BeginCollectionItem()의 매개 변수가 당신 속성의 이름과 일치해야하기 때문에 컬렉션이 바인딩되지 않는 이유가 있습니다. 당신은 또한 컬렉션에 AdhocViewModel 기존 렌더링하는 기본보기에서 루프를 필요 또한

@using (Html.BeginCollectionItem("ListAdhoc")) // binds to List<AdhocViewModel> ListAdhoc 

로 변경합니다. 초기에 존재하지 않더라도 ModelState이 유효하지 않으므로보기를 반환해야 할 경우를 대비하여 여전히 필요합니다. 기본보기에서 다음을 포함하십시오.

<div class="panel panel-success" id="adhoc"> 
    @foreach(var item in Model.ListAdhoc) 
    { 
     @Html.Partial("Adhoc", item) 
    } 
</div>