,究竟是什么,做了ModelBinder的呢?如何有效地使用它呢? [英] What, exactly, does a modelbinder do? How to use it effectively?

查看:161
本文介绍了,究竟是什么,做了ModelBinder的呢?如何有效地使用它呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究的东西,碰到的这个博客帖子在buildstarted.com了解模型粘合剂。它的实际工作pretty织补以及我的目的,但我不知道到底怎么回事幕后。我所做的就是创建调用自定义ModelBinder的 USerModelBinder

I was researching something and came across this blog post at buildstarted.com about model binders. It actually works pretty darn well for my purposes but I am not sure exactly whats going on behind the scenes. What I did was create a custom ModelBinder called USerModelBinder:

public class UserModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        ValueProviderResult value = bindingContext.ValueProvider.GetValue("id");
        MyEntities db = new MyEntities();
        User user = db.Users.SingleOrDefault(u => u.UserName == value.AttemptedValue);
        return user;
    }
}

然后在我的的Global.asax.cs 我有:

ModelBinders.Binders.Add(typeof运算(用户),新UserModelBinder());

我的理解是,使用模型绑定让我没有使用以下行中,涉及到用户每个控制器的动作。因此,除了传递一个ID的动作时,ModelBinder的拦截ID,获取正确的项目(在我的例子用户),并将其转发到进行处理的操作。

My understanding is that using the model binder allows me to NOT have to use the following lines in every controller action that involves a "User". So instead of passing in an "id" to the action, the modelbinder intercepts the id, fetches the correct "item"(User in my case) and forwards it to the action for processing.

        MyEntities db = new MyEntities();
        User user = db.Users.SingleOrDefault(u => u.UserName == value.AttemptedValue);

我也用我的User类的注释,而不是使用G​​lobal.asax.cs中该行的尝试:

I also tried using an annotation on my User class instead of using the line in Global.asax.cs:

[ModelBinder(typeof(UserModelBinder))]
public partial class User
{
}

我不是在寻找一个30页的白皮书,但我不知道模型绑定如何做它做什么。我只是想了解,从当一个请求给它送达时作出会发生什么。所有这些东西只是工作是不能接受我,哈哈。此外,有没有使用注释与在Global.asax.cs中添加它有什么区别?他们似乎工作一样在我的测试,但有什么陷阱?

I'm not looking for a 30 page white paper but I have no idea how the model binder does what it does. I just want to understand what happens from when a request is made to the time it is served. All this stuff "just working" is not acceptable to me, lol. Also, is there any difference between using the annotation versus adding it in Global.asax.cs? They seem to work the same in my testing but are there any gotchas?

推荐答案

通常情况下,模型绑定(在MVC)看着你操作方法,并认为它需要(如,对象类型)。然后,它试图寻找从HTTP请求中的值(在HTTP表单值,查询字符串,JSON也许其他地方,如使用ValueProviders饼干等)。然后,它创建它检索的参数的新对象。

Usually the Model Binder (in MVC) looks at you Action method and sees what it requires (as in, the objects types). It then tries to find the values from the HTTP Request (values in the HTTP Form, QueryString, Json and maybe other places such as cookies etc. using ValueProviders). It then creates a new object with the parameters that it retrieves.

IMO你所做的是不是真的模型绑定。在您刚才读的ID和从DB。

IMO What you've done is not really "model binding". In the sense that you've just read the id and fetched the object from the DB.

例如常用的模型绑定:

// class
public class SomeClass
{
    public int PropA {get;set;}
    public string PropB {get;set;}
}

// action

public ActionResult AddSomeClass(SomeClass classToBind)
{
  // implementation
}

// pseudo html

      <form action="">
       <input name="PropA" type="text" />
       <input name="PropB" type="text" />
      </form>

如果您发布包含正确的值(假设你使用发布和PropA一个PROPB形式)的模型绑定可以识别您在形式发送这些值,并建立一个SomeClass的对象的形式。

if you post a form that contains the correct values (lets say you post a form with PropA and PropB ) the model binder can identify that you've sent those values in the form and build a SomeClass object.

如果你真的想打造你应该使用强类型查看和使用的HtmlHelper的EditorFor(或EditorForModel)创建所有MVC需要正确的名称一个真实的工作示例。

If you really want to create a real working example you should use a strongly typed View and use HtmlHelper's EditorFor (or EditorForModel) to create all the correct names that MVC needs.

-

参考MVC的默认粘结剂是<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder.aspx\">DefaultModelBinder,还有一些(有更多的,你可以看看周围的System.Web.Mvc命名空间)ValueProviders它默认使用的是<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.formvalueprovider.aspx\">FormValueProvider和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.querystringvalueprovider.aspx\">QueryStringValueProvider

for reference MVC's default binder is the DefaultModelBinder, and some (there are more, you can look around in the System.Web.Mvc namespace) ValueProviders that it uses by default are the FormValueProvider and the QueryStringValueProvider

所以,我已经说了,怎么这主要工作原理是,默认的模型绑定读取的动作recieving模型(比如SomeClass的在本例中)在读什么是它可以读取值(说PropA和PROPB)并要求ValueProviders为属性的正确值。

So, as I already said, how this basically works is that the default model binder reads the model that the action is recieving (say SomeClass in the example) reads what are the values that it can read (say PropA and PropB) and asks the ValueProviders for the correct values for the properties.

另外,如果我没有记错,你也可以使用ValueProviderFactories静态类看到价值提供商在运行。

Also, if I recall correctly, you can also see the value providers in runtime using the ValueProviderFactories static class.

这篇关于,究竟是什么,做了ModelBinder的呢?如何有效地使用它呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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