2014-02-21 37 views
3

考虑一个动态的调查形式的ñ问题并为每个问题consit可以有ň回答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> 

是否有可能有MVC解析它上提交到类似的结构:

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; } 
} 

如果是这样怎么样?

EDIT(如答案建议):

@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是空

+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

似乎不起作用。首先,这是我的问题。你如何将这个表单序列化为一个post请求? - 为了更精确地将模型中的Questions属性设置为空 –

+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

似乎没有工作。首先,这是我的问题。你将如何将这个表单序列化为一个post请求? - 为了更加精确地将模型中的Questions属性设置为null –