用于派生对象列表的 MVC2 Modelbinder

发布于 2024-08-24 04:37:13 字数 999 浏览 11 评论 0原文

我想要一个与 Asp.net MVC 2 中的默认 Modelbinder 一起使用的不同(派生)对象类型的列表。

我有以下 ViewModel:

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; }                  
    }

并且该列表包含不同派生类型的对象,例如,

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

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

如果我使用 List 并不重要或者 ArrayList 实现,所有内容都在表单中很好地搭建起来,但是在回发到 ActionResult 时,模型绑定程序不会为我解析派生对象类型属性。

对于 Viewmodel 结构来说,处理不同对象类型的列表有什么好的解决方案?为每个对象类型(例如列表、列表等)提供一个额外的列表对我来说似乎不是一个好的解决方案,因为这在构建视图模型并将其映射回域模型方面都会产生大量开销。

考虑在自定义模型绑定器中绑定所有属性的其他方法,如何在不产生大量开销的情况下使用此处的数据注释方法(验证所需属性等)?

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

I have the following ViewModel:

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; }                  
    }

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; }
    }

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-08-31 04:37:13

查看MvcContrib 中的派生类型 ModelBinder。这允许您通过“类型标记”过程对派生类型进行模型绑定 - 当使用 RenderTypedPartial(...) 帮助器时,该过程会自动为您处理。 MvcContrib 部分维护跨部分的绑定状态,以便在深层对象图上正确维护名称/Id 前缀。如果您使用模板等其他机制,那么您需要自己处理类型标记。文档页面对此进行了解释。

回到您的问题以及如何使用 ModelBinder 解析派生类型,您可以通过类似于 WCF KnownTypeAttribute 的机制使用属性注册派生类型变体,也可以在启动时进行注册。无论哪种方式,这些变化都会注册一次并出于性能考虑而保留。

模型绑定器还以不干扰数据注释/验证属性的方式解决了这个问题。在任何其他情况下,它们都会按照您的预期工作。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文