【发布时间】:2016-05-10 14:45:03
【问题描述】:
我正在尝试发布一个包含复杂类型字典的表单。 字典是这样的:
public Dictionary<Question, List<QuestionAlternative>> Questions { get; set; }
一个问题可以有多种选择。
问题本身是这样的:
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; }
}
然后是 QuestionAlternative:
public class QuestionAlternative
{
public Guid Id { get; set; }
public string Alternative { get; set; }
public Guid QuestionId { get; set; }
public Question Question { get; set; }
}
我的表单如下所示:
<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>
我发布到的 ActionResult 如下所示:
[AllowAnonymous]
[HttpPost("AnswerMe")]
public IActionResult AnswerMe([FromBody]AnswerViewModel model)
{
foreach(var item in model.Questions) //throws exception
....
}
和我的视图模型:
public class AnswerViewModel
{
public ODELDAL.Entities.Test Test { get; set; }
public Dictionary<Question, List<QuestionAlternative>> Questions { get; set; }
public Guid SelectedRole { get; set; }
}
当我发布表单时,名为 Questions 的字典为空。
是否可以使用默认的模型绑定器来实现这一点,或者我是否需要构建一个自定义模型绑定器。
如果是这样,该解决方案会是什么样子?
提前致谢
【问题讨论】:
-
您的 HttpPost 操作看起来如何?
-
@Shyju 编辑了我的问题
-
所以你有一个包含
QuestionAlternatives 集合的字典,每个QuestionAlternative都有一个Question,它本身有一个QuestionAlternatives 集合。这种嵌套(以及在某种程度上,绑定)可以永远持续下去的意图是什么? -
@WillRay 这部分只是实体在 EF 中的关联和绑定方式(我首先使用代码),您现在可以忽略它。
-
在表单发布时检查您的请求(Fiddler 或浏览器开发工具)。虽然表单将发布键值对,但您的场景更为复杂。建议:既然
Question已经拥有List或QuestionAlternative,为什么不在您的视图模型中使用简单的收集(列表、数组...等)。
标签: c# asp.net-mvc dictionary model-binding