首页 > 代码库 > MVC中的默认Model绑定者DefaultModelBinder
MVC中的默认Model绑定者DefaultModelBinder
一、题外话
接续上一篇:
MVC中Action参数绑定的过程
public IModelBinder DefaultBinder{ get { if (this._defaultBinder == null) { this._defaultBinder = new DefaultModelBinder(); } return this._defaultBinder; } set { this._defaultBinder = value; }}
二、分析DefaultModelBinder
public class DefaultModelBinder : IModelBinder{ public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext){}//其他方法我们暂时不分心,不止这一个方法}
public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext){ EnsureStackHelper.EnsureStack(); if (bindingContext == null) { throw new ArgumentNullException("bindingContext"); } bool flag = false; if (!string.IsNullOrEmpty(bindingContext.ModelName) && !bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName)) {//ValueProvider是一个IValueProvider的实例,我们下面分析一下这个东西 if (!bindingContext.FallbackToEmptyPrefix) { return null; } bindingContext = new ModelBindingContext { ModelMetadata = bindingContext.ModelMetadata, ModelState = bindingContext.ModelState, PropertyFilter = bindingContext.PropertyFilter, ValueProvider = bindingContext.ValueProvider }; flag = true; } if (!flag) { bool flag2 = DefaultModelBinder.ShouldPerformRequestValidation(controllerContext, bindingContext); IUnvalidatedValueProvider arg_9B_0 = bindingContext.UnvalidatedValueProvider; bool skipValidation = !flag2; ValueProviderResult value = arg_9B_0.GetValue(bindingContext.ModelName, skipValidation); if (value != null) { return this.BindSimpleModel(controllerContext, bindingContext, value); } } if (!bindingContext.ModelMetadata.IsComplexType) { return null; } return this.BindComplexModel(controllerContext, bindingContext);}
看看IValueProvider
public interface IValueProvider { /// <summary>Determines whether the collection contains the specified prefix.</summary> /// <returns>true if the collection contains the specified prefix; otherwise, false.</returns> /// <param name="prefix">The prefix to search for.</param> bool ContainsPrefix(string prefix); /// <summary>Retrieves a value object using the specified key.</summary> /// <returns>The value object for the specified key.</returns> /// <param name="key">The key of the value object to retrieve.</param> ValueProviderResult GetValue(string key); }
可以看看这篇博文:http://www.cnblogs.com/artech/archive/2012/05/17/value-provider-01.html
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。