我正在阅读一篇关于 Value Provider and Model Binder 之间有什么区别的文章.
我了解模型绑定(bind)器基本上获取传入数据并构建对象。假设我正在发送学生数据,然后模型绑定(bind)器在从帖子到服务器并构建学生对象时抓取传入的学生数据。
但是还是不明白MVC中Value Provider的工作是什么。
那么请用简单的示例解释一下 Value Provider 做了什么样的工作以及模型绑定(bind)器做了什么?
不清楚 haacked.com 在说什么
The DefaultModelBinder will pull the Id value from the RouteData and the Age, FirstName, and LastName values from the JSON when building up the Person object. Afterwards, it’ll perform validation without having to know that the various values came from different sources.
当我发布 id、name、age 等时,为什么模型绑定(bind)器将仅从路由数据中选择 id,而从 JSON 中选择其余数据。模型绑定(bind)器应该从 RouteData 中选择所有值或从 JSON 中选择所有值.......那么为什么只选择 id 呢?
mvc 中存在多少种不同类型的值提供者?
最佳答案
值(value)提供者 用于从请求中的不同来源获取数据
模型绑定(bind)器 从 ValueProviders 获取数据以创建一个对象。他们不会直接从 Route Data、JSON 或某些特定数据中获取它,这不是他们的责任。
ChildActionValueProviderFactory
FormValueProviderFactory
JsonValueProviderFactory
RouteDataValueProviderFactory
QueryStringValueProviderFactory
HttpFileCollectionValueProviderFactor
QueryStringValueProviderFactory
RouteDataValueProviderFactory
引用:专业 ASP.Net MVC 5,Wrox 版中的 Brad Wilson
多个
ValueProviders
可以合作从不同来源获取数据。如果多次提供数据,则优先级很重要。
例如: id 来自 querystring 和 RouteData - RouteData 胜过 querystring
祝一切顺利
关于asp.net-mvc - ASP.Net MVC : What’s the Difference Between a Value Provider and Model Binder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35798726/