何时使用IModelBinder与DefaultModelBinder [英] When to use IModelBinder versus DefaultModelBinder

查看:232
本文介绍了何时使用IModelBinder与DefaultModelBinder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个意愿,不会使用默认模型绑定工作模型中的MVC3站点。我一直在寻找在线code样品,看来我可以创建一个自定义的模型绑定,要么实现IModelBinder或DefaultModelBinder继承。有人能解释每一种方法的优点/缺点,当一种方法是使用,而不是其他的可能的例子。

先谢谢了。


解决方案

这两种方法实际上是同一个:DefaultModelbinder实现IModelBinder,因此继承了它是一种不如另一个实施IModelBinder

临从DefaultModelBinder继承:您可以重复使用很多来自DefaultModelBinder的行为,并覆盖只有你想要的。您不必从头来实现。

专业制作自己的实现IModelBinder的:你只有一个方法来实现( IModelBinder.BindModel ),你有过你的实现是做完全控制<。 / p>

正确的方法在很大程度上取决于你从定制绑定需要什么,但是DefaultModelBinder的行为通常是你所需要的(在大多数情况下,普通的旧DefaultModelBinder确实是你想要的粘合剂)。

I've got an MVC3 site that will has a model that won't work with default model binding. I've been looking at code samples on line, and it appears I could create a custom model binder that either implements IModelBinder or inherits from DefaultModelBinder. Can someone explain the pros/cons of each approach, and possibly example of when one approach would be used rather than the other.

Thanks in advance.

解决方案

The two approaches are in fact the same one: DefaultModelbinder implements IModelBinder, so inheriting from it is a way as good as another to implement IModelBinder.

Pro for inheriting from DefaultModelBinder: you can reuse a lot of the behaviors from DefaultModelBinder, and override only the ones you want. You don't have to implement from scratch.

Pro for making your own implementation of IModelBinder: you only have one method to implement (IModelBinder.BindModel) and you have full control over what your implementation is doing.

The correct way largely depends on what you need from your custom binder, but the behavior of the DefaultModelBinder is usually what you need (and in most cases, plain old DefaultModelBinder is indeed the binder you want).

这篇关于何时使用IModelBinder与DefaultModelBinder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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