ASP.Net MVC ModelBindingContext分类 - 如何填充它的模型值? [英] ASP.Net MVC ModelBindingContext class-- how are its model values populated?

查看:161
本文介绍了ASP.Net MVC ModelBindingContext分类 - 如何填充它的模型值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我抓我的头有点如何模型粘合剂做他们在ASP.Net MVC的工作。

I'm scratching my head a bit at how model binders do their work in ASP.Net MVC.

要具体而言,BindModel()方法保存的型号名称和类型ModelBindingContext参数,但我不明白的ModelBindingContext如何接收这些值。

To be specific, the BindModel() method has a ModelBindingContext parameter that holds the model name and type, but I don't understand how the ModelBindingContext receives these values.

这是MVC模式必须从提交的表单值或查询字符串参数或其他数据源填充。但是,什么机制决定交给ModelBindingContext模型类型,以及如何选择是对另一机型一个模型类,甚至多(说)含个体的简单榜值?

An MVC model has to be populated from posted form values or query string parameters, or other sources of data. But what mechanism determines the model type handed to the ModelBindingContext, and how is one model type chosen over another model type, over even (say) a simple list containing the individual posted values?

它只是似乎我ModelBindingContext知道模型的类型它被递给了,我不知道在哪里了的来自或涉及填充它的工作流程。

It just appears to me the ModelBindingContext "knows" the type of model it's being handed, and I'm not sure where that's coming from or the workflow involved in populating it.

推荐答案

有趣的问题。这里是什么MVC做了一个简单的概述。这一切都被ControllerActionInovker类处理。这不是在特定的顺序,但靠近

Interesting question. Here is a simple overview of what MVC does. It's all handled by the ControllerActionInovker class. This is not in specific order, but is close.


  1. ControllerActionInovker决定通过反射的参数类型。

  2. 接下来ValueProviders从HttpContext的申请表,路径,查询字符串,等等属性创建。您也可以提供自己的价值提供者。

  3. 这些ValueProviders经由充当虚拟ValueProvider集合提供给ModelBindingContext

  4. 然后ControllerActionInovker查找的特定类型ModelBinder的。如果它没有找到一个默认为内置的DefaultModelBinder。

  5. 在大多数情况下,所使用的DefaultModelBinder。它的任务是创建一个模型,并使用ValueProviders的性能与使用模特属性名称作为键值连接。当ValueProviders有一个值,它们返回一个ValueProviderResult对象,它是负责类型转换。

您可以在位于codeplex.com < MVC源码看到自己/ A>。查找ControllerActionInvoker类和GetParameterValue方法。

You can see this for yourself in the ASP.net MVC source located at codeplex.com. Look for the ControllerActionInvoker class and the GetParameterValue method.

这篇关于ASP.Net MVC ModelBindingContext分类 - 如何填充它的模型值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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