我试图发布一个包含复杂类型字典的表单。 字典看起来像这样:
<script src=..."
一个问题可以有很多选择。
问题本身如下:
public Dictionary<Question, List<QuestionAlternative>> Questions { get; set; }
然后是QuestionAlternative:
public class Question
{
public Guid Id { get; set; }
public string Text { get; set; }
public Guid TestId { get; set; }
public Test Test { get; set; }
public ICollection<QuestionAlternative> QuestionAlternatives { get; set; }
public Guid? QuestionTypeId { get; set; }
public QuestionType QuestionType { get; set; }
}
我的表单如下:
public class QuestionAlternative
{
public Guid Id { get; set; }
public string Alternative { get; set; }
public Guid QuestionId { get; set; }
public Question Question { get; set; }
}
我发布的ActionResult看起来像这样:
<form asp-controller="Answer" asp-action="AnswerMe" method="post">
@foreach (var questionPair in Model.Questions)
{
@Html.Hidden("Questions[" + questionIndex + "].Key.Id", questionPair.Key.Id)
<br />
foreach (var alternative in questionPair.Value)
{
<label for="@alternative.Id">@questionPair.Key.Text</label>
<textarea id="@alternative.Id" name="@("Questions["+ questionIndex + "].Value["+ index +"]")"></textarea>
index++;
}
}
questionIndex++;
}
<input type="submit" class="button" value="Answer" />
</form>
和我的viewmodel:
[AllowAnonymous]
[HttpPost("AnswerMe")]
public IActionResult AnswerMe([FromBody]AnswerViewModel model)
{
foreach(var item in model.Questions) //throws exception
....
}
当我发布我的表单时,名为Questions的字典为空。
是否可以使用默认的模型绑定器实现此目的,或者我是否需要构建自定义模型。
如果是这样,该解决方案将如何?
提前致谢
答案 0 :(得分:1)
由于Question
已经拥有QuestionAlternative
的集合,为什么不在视图模型中使用简单的集合(List,数组......等)...
public class AnswerViewModel {
public ODELDAL.Entities.Test Test { get; set; }
public Question[] Questions { get; set; }
public Guid SelectedRole { get; set; }
}
......以及更新的视图......
<form asp-controller="Answer" asp-action="AnswerMe" method="post">
@for(int i = 0; i < Model.Questions.Length; i++) {
@Html.HiddenFor(m => m.Questions[i].Id)
<br />
for(int j = 0; j < m.Questions[i].QuestionAlternatives.Count; j++) {
@Html.LabelFor(m => m.Questions[i].QuestionAlternatives[j].Alternative,m.Questions[i].QuestionAlternatives[j].Text)
@Html.TextAreaFor(m => m.Questions[i].QuestionAlternatives[j].Alternative)
}
}
<input type="submit" class="button" value="Answer" />
</form>
模型绑定器将使用此方法更轻松地重建模型,因为它将能够使用表达式为html标记生成ID。
例如,隐藏的输入在生成时看起来像这样
<input data-val="true" id="Questions_0__Id" name="Questions[0].Id" value="{Some-Guid-Value_Here}" type="hidden">