MVC2 ModelBinder的派生对象的列表 [英] MVC2 Modelbinder for List of derived objects

查看:141
本文介绍了MVC2 ModelBinder的派生对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Asp.net MVC 2默认ModelBinder的工作不同(派生)对象类型的列表。

I want a list of different (derived) object types working with the Default Modelbinder in Asp.net MVC 2.

我有以下视图模型:

public class ItemFormModel
    {       
        [Required(ErrorMessage = "Required Field")] 
        public string Name { get; set; }
        public string Description { get; set; }

        [ScaffoldColumn(true)]
        //public List<Core.Object> Objects { get; set; }       
        public ArrayList Objects { get; set; }                  
    }

和列表中包含diffent派生类型的对象,例如

And the list contains objects of diffent derived types, e.g.

public class TextObject : Core.Object
    {
        public string Text { get; set; }
    }

    public class BoolObject : Core.Object
    {
        public bool Value { get; set; }
    }

如果我使用List或ArrayList中实现,一切都很好地得到脚手架的形式,但回发到的ActionResult时ModelBinder的不为我解决派生的对象类型属性没关系。

It doesn't matter if I use the List or the ArrayList implementation, everything get's nicely scaffolded in the form, but the modelbinder doesn't resolve the derived object type properties for me when posting back to the ActionResult.

可能是什么视图模型结构的一个很好的解决方案来获得处理不同的对象类型的列表?具有每个对象类型(如列表,列表等),一个额外的名单似乎是对我来说不是一个好的解决方案,因为这两者在构建视图模型,并映射回领域模型有很多的开销。

What could be a good solution for the Viewmodel structure to get a list of different object types handled? Having an extra list for every object type (e.g. List, List etc.) seems to be not a good solution for me, since this is a lot of overhead both in building the viewmodel and mapping it back to the domain model.

有关在自定义的模型绑定绑定的所有属性的另一种方法思考,如何才能让我使用注解接近这里(验证所需的属性等)的数据,而无需大量的开销?

Thinking about the other approach of binding all properties in a custom model binder, how can I make use the data annotations approach here (validating required attributes etc.) without a lot of overhead?

推荐答案

查看<一个href=\"http://mvccontrib.$c$cplex.com/wikipage?title=DerivedTypeModelBinder&referringTitle=Documentation\"相对=nofollow>派生类型ModelBinder的在MvcContrib 。这允许您通过typestamping的过程modelbind到派生类型 - 这是使用RenderTypedPartial(...)当助手自动为您处理。 MvcContrib谐音保持整个谐音结合状态,这样的名称/ ID prefixes得到妥善保养上深深的对象图。如果你使用其他的机制,例如模板,那么你就需要处理typestamping自己。这是在文档页面解释说。

Check out the Derived Type ModelBinder in MvcContrib. This allows you to modelbind to derived types through the process of 'typestamping' - which is handled automatically for you when using the RenderTypedPartial(...) helper. MvcContrib partials maintain binding state across partials so the Name/Id prefixes are properly maintained on a deep object graph. If you use other mechanisms like templates, then you'll need to handle the typestamping yourself. This is explained in the documentation page.

再回到你的问题,并派生类型如何与ModelBinder的解决,可以在类似WCF KnownTypeAttribute机制注册的属性派生类型的变化,也可以做到在启动时注册。无论哪种方式,这些变化被注册一次,出于性能考虑,扶住。

Getting back to your questions and how the derived types are resolved with the ModelBinder, you can register the derived type variations with attributes in a mechanism similar to the WCF KnownTypeAttribute or you can do the registration on startup. Either way, these variations are registered once and held onto for performance considerations.

该模型粘合剂还解决了在不与数据注解干扰的方法这一问题/验证属性。如你希望他们在任何其他情况下,他们会工作。

The model binder also solves this problem in a way that does not interfere with data annotation/validation attributes. They will work as you expect them in any other scenario.

这篇关于MVC2 ModelBinder的派生对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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