MVC3查看抽象属性 [英] MVC3 View With Abstract Property

查看:115
本文介绍了MVC3查看抽象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下。

public class DemographicsModel
{
    public List<QuestionModel> Questions { get; set; }
}

//Does not work at all if this class is abstract.
public /*abstract*/ class QuestionModel
{
    //...
}

public class ChooseOneQuestionModel : QuestionModel
{
    //...
}

public class ChooseManyQuestionModel : QuestionModel
{
    //...
}

public class RichTextQuestionModel : QuestionModel
{
    //...
}

public class TextQuestionModel : QuestionModel
{
    //...
}

控制器

[HttpPost]
public ActionResult Demographics(DemographicsModel model)
{
    //...
}

我的观点将有一个 DemographicsModel 与所有的各种类型的众多问题,如上图所示。表格完成后和 POST ED回服务器,在问题的属性人口统计学模型被重新填入正确的一些问题,但他们的类型都是 QuestionModel 而不是具体类型。

My view would have a DemographicsModel with numerous question of all the varying types as shown above. After the form is completed and POSTed back to the server, the Questions property of the Demographics model is re-populated with the correct number of questions but they are all of type QuestionModel instead of the concrete type.

我如何做这件事情了解实例化是什么类型的?

How do I make this thing understand what type to instantiate?

推荐答案

如果我的根模型是一个抽象的是Frazell的答案会工作。然而,这并非如此,我还等什么结束了工作是这样的:<一href=\"http://mvccontrib.$c$cplex.com/wikipage?title=DerivedTypeModelBinder&referringTitle=Documentation\" rel=\"nofollow\">http://mvccontrib.$c$cplex.com/wikipage?title=DerivedTypeModelBinder&referringTitle=Documentation

Frazell's answer would have worked if my root model was the one that was abstract. However, that's not the case for me so what ended up working was this: http://mvccontrib.codeplex.com/wikipage?title=DerivedTypeModelBinder&referringTitle=Documentation

这篇关于MVC3查看抽象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆