考虑一个动态的调查形式的ñ问题并为每个问题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是空
你有过的标记控制? – Andrei
是的,我确实拥有对标记的控制 –