zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    bzoj2161 布娃娃
    bzoj2161 布娃娃
    Tyvj1054
    Tyvj1054
    Tyvj1053
    Tyvj1053
    hdu3265 Poster(扫描线)
    hdu3265 Poster(扫描线)
    hdu3265(好题翻译)
    hdu3265(好题翻译)
  • 原文地址:https://www.cnblogs.com/humble/p/3809696.html
Copyright © 2011-2022 走看看