0

I'm quite new to MVC3, just learning and am looking for some guidance.

For sake of simplicity, I have a model that represents 3 types of elements, questions, answers and containers.

All 3 inherit from a common base type, which I'll call baseElement.

When the model is delivered to the view it is a single object of type 'baseElement'

The container elements have an internal list of baseElements. Those baseElements can be of any of the three types. So - containers can contain questions, or containers (which could also contain questions, containers, etc..)

Each question can contain different types of answer types.

I'm trying to figure out how to use mvc3 to best implement a system to display this container/question structure to the user - permitting them to answer questions with various answer types while respecting the nested structure of the incoming model.

4

1 回答 1

0

好吧,尽管我的模型具有动态特性,但经过一段时间的探索后,我已经能够渲染我的模型对象结构而没有太多复杂性。

我通过使用强类型编辑器模板(每种类型一个)和视图中的以下代码来做到这一点。

@Html.EditorFor(x => @Model, @Model.GetType().Name)

这会根据实际类型自动选择要使用的正确编辑器模板。在每个类型特定的编辑器模板中,我对每个孩子都进行了相同的调用。

它实际上最终变得非常简单。

我现在遇到的一个大问题是,一旦用户发布表单值,如何将表单值绑定(或以其他方式检索)回可用的东西。结构的动态特性导致默认模型绑定器越过它的手臂并放弃。

在这一点上,我认为检索/重新映射表单数据可能是一个更大的问题,但这肯定需要更多的修补,也许是一个单独的问题。

谢谢您的帮助。:-)

于 2012-06-06T20:20:57.213 回答