[英]MVC4 Generic Collection model issue on POST
I'm trying to implement a basic Questionnaire engine that utilizes the Questionnaire model as below: 我正在尝试实现一个基本的问卷调查引擎,该引擎利用以下问卷调查模型:
public class Questionnaire
{
public ICollection<Step> Steps { get; set; }
}
public class Step
{
ICollection<IQuestion> Questions { get; set; }
}
public class TextBoxQuestion : IQuestion
{
public QuestionTypeEnum QuestionType { get; set; }
public string QuestionText { get; set; }
}
public class DropDownQuestion : IQuestion
{
public QuestionTypeEnum QuestionType { get; set; }
public string QuestionText { get; set; }
public IList<string> Options { get; set; }
}
QuestionEditor Template: QuestionEditor模板:
@model IList<Models.IQuestion>
@for (int i = 0; i < @Model.Count(); i++)
{
@Html.EditorFor(x => x[i], @Model[i].QuestionType.ToString());
}
TextBoxQuestion Editor: TextBoxQuestion编辑器:
public class TextBoxQuestion : IQuestion
{
public QuestionTypeEnum QuestionType { get; set; }
public string QuestionText { get; set; }
}
DropDownQuestion Editor: DropDownQuestion编辑器:
public class DropDownQuestion : IQuestion
{
public QuestionTypeEnum QuestionType { get; set; }
public string QuestionText { get; set; }
}
Roughly following Jon Egerton's example here I'm implementing a generic ICollection of IQuestion interface. 在这里大致遵循Jon Egerton的示例,我正在实现IQuestion接口的通用ICollection。 MVC interprets each IQuestion by it's type and renders either the TextBoxQuestion or DropDownQuestion editor template accordingly.
MVC通过其类型解释每个IQuestion,并相应地呈现TextBoxQuestion或DropDownQuestion编辑器模板。 Great.
大。
However when the Questionnaire model is post back to the Controller the IQuestion collections are all null. 但是,当将调查表模型回发到Controller时,IQuestion集合全为空。
Can anyone help me with advice on whether there is a better approach or whether I can hook up a DependencyResolver or something else so that MVC can interpret my IQuestion objects back as TextBoxQuestion and DropDownQuestion types? 任何人都可以向我提供有关是否有更好方法的建议,或者是否可以连接DependencyResolver或其他方法,以便MVC可以将IQuestion对象解释为TextBoxQuestion和DropDownQuestion类型?
Hope this explanation is clear, feel free to ask any questions. 希望这个解释很清楚,随时问任何问题。
thanks all in advance 提前谢谢大家
Mark 标记
Found a resolution for this if anyone is trying to achieve the same. 如果有人试图实现这一目标,则找到解决方案。
This article on MSDN Magazine was extremely useful. MSDN杂志上的这篇文章非常有用。
It explains some key concepts I didn't understand such as Model Binding is recursive so the processing traverses the entire object graph and applies a Model Binder for each Type. 它解释了一些我不了解的关键概念,例如“模型绑定”是递归的,因此处理过程遍历整个对象图并为每种类型应用“模型绑定器”。
The work through on the General Purpose Abstract Model Binder was the solution I used along with the custom AbstractModelBinderProvider. 通用抽象模型绑定器上的工作是我与自定义AbstractModelBinderProvider一起使用的解决方案。
Hope this helps someone 希望这可以帮助某人
Mark 标记
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.