简体   繁体   English

一般使用asp.net MVC模型联编程序

[英]Using asp.net mvc model binders generically

I have a hierarchy of classes that all derive from a base type and the base type also implements an interface. 我有一个类的层次结构,所有这些类都派生自基本类型,并且基本类型也实现了一个接口。 What I'm wanting to do is have one controller to handle the management of the entire hierarchy (as the actions exposed via the controller is identical). 我想要做的是有一个控制器来处理整个层次结构的管理(因为通过控制器公开的动作是相同的)。 That being said, I want to have the views have the type specific fields on it and the model binder to bind against a hidden field value. 话虽这么说,我想让视图上具有类型特定的字段,并具有绑定到隐藏字段值的模型绑定程序。 something like: 就像是:

<input type="text" name="model.DerivedTypeSpecificField" />
<input type="hidden" name="modelType" value="MyDerivedType" />

That being said, the asp.net mvc model binders seem to require the concrete type that they will be creating, because of that reason I would need to create a different controller for every derived type. 话虽如此,asp.net mvc模型联编程序似乎需要它们将要创建的具体类型,因为这个原因,我需要为每种派生类型创建一个不同的控制器。

Has anyone does this before or know how to manipulate the model binder to behave in this way? 有没有人以前这样做或知道如何操纵模型绑定器以这种方式工作? I could write my own model binder, but I'm not wanting anything past the basic model binding behavior of assign properties and building arrays on the target type. 我可以编写自己的模型绑定程序,但是除了分配属性和在目标类型上构建数组的基本模型绑定行为之外,我什么都不想要。

Thanks! 谢谢!

Don't know if it will work, but you can try something like: 不知道它是否会起作用,但是您可以尝试执行以下操作:

public class MyBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        bindingContext.ModelType = System.Type.GetType(controllerContext.HttpContext.Request["modelType"]);

        return base.BindModel(controllerContext, bindingContext);
    }
}

That is just simple change of basic model binder. 这只是基本模型活页夹的简单更改。

Check out the Derived Type ModelBinder in MvcContrib. 在MvcContrib中签出派生类型ModelBinder。 This allows you to modelbind to derived types through the process of 'typestamping' - which is handled automatically for you when using the RenderTypedPartial(...) helper. 这使您可以通过'typestamping'的过程将模型绑定到派生类型-使用RenderTypedPartial(...)帮助程序时会自动为您处理。 MvcContrib partials maintain binding state across partials so the Name/Id prefixes are properly maintained on a deep object graph. MvcContrib局部函数在各个局部函数之间保持绑定状态,因此可以在深层对象图上正确维护Name / Id前缀。 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. 回到问题以及如何使用ModelBinder解决派生类型,您可以使用类似于WCF KnownTypeAttribute的机制来注册带有属性的派生类型变体,也可以在启动时进行注册。 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. 在任何其他情况下,它们都将按您期望的那样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM