2014-02-21 3 views
3

는 MVC 제출에 그것을 구문 분석이 가질 수 있습니다 N 질문 및 각 질문에 대한 consit이 n은MVC 모델

<form> 
<!-- Question block --> 
    <input id="QuestionText" name="QuestionText" placeholder="Text otázky" value="" type="text"> 
    <table> 
     <!-- Answer block --> 
     <tr><td><input name="AnswerIsCorrect" type="checkbox"></td> 
      <td><input name="AnswerText" type="text"></td> 
     </tr> 
     <!-- Answer block END --> 
     <tr><td><input name="AnswerIsCorrect" type="checkbox"></td> 
      <td><input name="AnswerText" type="text"></td> 
     </tr> 
     <!-- More answers --> 
    </table> 
    <input id="QuestionComment" type="text"> 
    <!-- Question block END --> 

    <!-- More questions --> 
</form> 

응답이있을 수있는 동적 설문 조사 양식을 고려과 유사한 구조의 예 :

public class CreateSurveyModel 
{ 
    public List<QuestionModel> Questions { get; set; } 
} 
public class QuestionModel 
{ 
    public string QuestionText { get; set; } 
    public string QuestionComment { get; set; } 
    public List<AnswerModel> Answers { get; set; } 
} 
public class AnswerModel 
{ 
    public string AnswerText { get; set; } 
    public bool IsCorrect { get; set; } 
} 

그렇다면 어떻게 될까요? (답변에서 제안)

편집 :

@using(Html.BeginForm("Send", "Try", FormMethod.Post/*or FormMethod.Get*/)) 
{ 
    foreach(var question in Model.Questions) 
    { 
     <!-- Question block --> 

     @Html.TextBox("QuestionText", question.QuestionText)    
     <table> 
      @foreach(var answer in question.Answers) 
      { 
       <!-- Answer block --> 
       <tr> 
        <td>@Html.CheckBox("AnswerIsCorrect", answer.IsCorrect)</td> 
        <td>@Html.TextBox("AnswerText", answer.AnswerText)</td> 
       </tr> 
       <!-- Answer block END --> 
      } 
     </table> 
     @Html.TextBox("QuestionComment", question.QuestionComment) 
     <!-- Question block END --> 
    } 
    <input type="submit"/> 
} 

그리고 조치 :

[HttpPost] 
public ActionResult Send(CreateSurveyModel model) 
{ 
    return Index(); 
} 

그러나 model.Questions가 null

+0

당신은 마크 업을 통해 제어 할 수 있습니까? – Andrei

+0

예 마크 업을 제어 할 수 있습니다. –

답변

1

사용하여보기에서 다음과 같은 구조 :

@using (Html.BeginForm()) 
{ 
    for (int i = 0; i < Model.Questions.Count(); i++) 
    { 
     @Html.TextBoxFor(model => model.Questions[i].QuestionText, new { placeholder = "Text otázky" }) 
     <table> 
      @for (int j = 0; j < Model.Questions[i].Answers.Count(); j++) 
      { 
       <!-- Answer block --> 
       <tr> 
        <td>@Html.CheckBoxFor(model => model.Questions[i].Answers[j].IsCorrect)</td> 
        <td>@Html.TextBoxFor(model => model.Questions[i].Answers[j].AnswerText)</td> 
       </tr> 
      } 
     </table> 
     @Html.TextBoxFor(model => model.Questions[i].QuestionComment) 
     <!-- Question block END --> 
    } 
    <input type="submit"/> 
} 

이 코드는보기를 올바르게 채우고 모델을보기에 올바르게 전달하는 데 도움이됩니다.

업데이트

다음과 같은 작업 방법

으로 테스트 할 수 있습니다
public ActionResult Send() 
{ 
    CreateSurveyModel model = new CreateSurveyModel(); 
    model.Questions = new List<QuestionModel>() 
    { 
     new QuestionModel() 
     { 
      QuestionText = "1", 
      QuestionComment = "Comment 1", 
      Answers = new List<AnswerModel>() 
      { 
       new AnswerModel() 
       { 
        AnswerText = "A1", 
        IsCorrect = false, 
       }, 
       new AnswerModel() 
       { 
        AnswerText = "A2", 
        IsCorrect = true, 
       }, 
       new AnswerModel() 
       { 
        AnswerText = "A3", 
        IsCorrect = false, 
       }, 
       new AnswerModel() 
       { 
        AnswerText = "A4", 
        IsCorrect = true, 
       }, 
      } 
     }, 
     new QuestionModel() 
     { 
      QuestionText = "2", 
      QuestionComment = "Comment 2", 
      Answers = new List<AnswerModel>() 
      { 
       new AnswerModel() 
       { 
        AnswerText = "A5", 
        IsCorrect = false, 
       }, 
       new AnswerModel() 
       { 
        AnswerText = "A6", 
        IsCorrect = false, 
       }, 
       new AnswerModel() 
       { 
        AnswerText = "A7", 
        IsCorrect = false, 
       }, 
       new AnswerModel() 
       { 
        AnswerText = "A8", 
        IsCorrect = true, 
       }, 
       new AnswerModel() 
       { 
        AnswerText = "A9", 
        IsCorrect = false, 
       }, 
      } 
     } 
    }; 

    return View(model); 
} 

[HttpPost] 
public ActionResult Send(CreateSurveyModel model) 
{ 
    return View(); 
} 

참고 : 당신이보기에 당신은 어떤 데이터를 채우는 것을 잊지 마십시오.

+0

작동하지 않는 것 같습니다. 이것은 처음부터 내 질문입니다. 이 양식을 게시물 요청으로 어떻게 직렬화 하시겠습니까? - 더 정확 해지기 위해 모델의 Questions 속성은 게시물에 null이 아닙니다. –

+0

무엇을 의미합니까? 추가 도움을 줄 수있는 행동 방법을 보여주세요. – ssimeonov

+0

질문이 수정되었습니다 –

0

이 작업을 수행 할 수 있습니다

@using(Html.BeginForm("action", "controller", FormMethod.Post/*or FormMethod.Get*/)) 
{ 
    foreach(var question in Model.Questions) 
    { 
     <!-- Question block --> 
     @Html.TextBoxFor("QuestionText", question.QuestionText)    
     <table> 
      @foreach(var answer in question.Answers) 
      { 
       <!-- Answer block --> 
       <tr> 
        <td>@Html.Checkbox("AnswerIsCorrect", answer.IsCorrect)</td> 
        <td>@Html.TextBox("AnswerText", answer.AnswerText)</td> 
       </tr> 
       <!-- Answer block END --> 
      } 
     </table> 
     @Html.TextBox("QuestionComment", question.QuestionComment) 
     <!-- Question block END --> 
    } 
} 
+1

작동하지 않는 것 같습니다. 이것은 처음부터 내 질문입니다. 이 양식을 게시물 요청으로 직렬화하는 방법은 무엇입니까? - 모델의 질문 속성을보다 정확하게하려면 게시물에 대해 null입니다. –