/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. All rights reserved.
 *
 * This software is subject to the Microsoft Public License (Ms-PL). 
 * A copy of the license can be found in the license.htm file included 
 * in this distribution.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * ***************************************************************************/namespace System.Web.Mvc {
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Web.Mvc.Resources;    [SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable",
        Justification = "It is not anticipated that users will need to serialize this type.")]
    [SuppressMessage("Microsoft.Design", "CA1035:ICollectionImplementationsHaveStronglyTypedMembers",
        Justification = "It is not anticipated that users will call FormCollection.CopyTo().")]
    [FormCollectionBinder]
    public class FormCollection : NameValueCollection {        public FormCollection() {
        }        public FormCollection(NameValueCollection collection) {
            if (collection == null) {
                throw new ArgumentNullException("collection");
            }            Add(collection);
        }        public IDictionary<string, ValueProviderResult> ToValueProvider() {
            CultureInfo currentCulture = CultureInfo.CurrentCulture;            Dictionary<string, ValueProviderResult> dict = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);
            string[] keys = AllKeys;
            foreach (string key in keys) {
                string[] rawValue = GetValues(key);
                string attemptedValue = this[key];
                ValueProviderResult vpResult = new ValueProviderResult(rawValue, attemptedValue, currentCulture);
                dict[key] = vpResult;
            }            return dict;
        }        public virtual ValueProviderResult GetValue(string name) {
            if (String.IsNullOrEmpty(name)) {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
            }            string[] rawValue = GetValues(name);
            if (rawValue == null) {
                return null;
            }            string attemptedValue = this[name];
            return new ValueProviderResult(rawValue, attemptedValue, CultureInfo.CurrentCulture);
        }        private sealed class FormCollectionBinderAttribute : CustomModelBinderAttribute {            // since the FormCollectionModelBinder.BindModel() method is thread-safe, we only need to keep
            // a single instance of the binder around
            private static readonly FormCollectionModelBinder _binder = new FormCollectionModelBinder();            public override IModelBinder GetBinder() {
                return _binder;
            }            // this class is used for generating a FormCollection object
            private sealed class FormCollectionModelBinder : IModelBinder {
                public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
                    if (controllerContext == null) {
                        throw new ArgumentNullException("controllerContext");
                    }                    return new FormCollection(controllerContext.HttpContext.Request.Form);
                }
            }
        }    }
}
设计模式mvc

解决方案 »

  1.   

    1、嵌套类  
     private sealed class FormCollectionBinderAttribute : CustomModelBinderAttribute {            // since the FormCollectionModelBinder.BindModel() method is thread-safe, we only need to keep
                // a single instance of the binder around
                private static readonly FormCollectionModelBinder _binder = new FormCollectionModelBinder();            public override IModelBinder GetBinder() {
                    return _binder;
                }            // this class is used for generating a FormCollection object
                private sealed class FormCollectionModelBinder : IModelBinder {
                    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
                        if (controllerContext == null) {
                            throw new ArgumentNullException("controllerContext");
                        }                    return new FormCollection(controllerContext.HttpContext.Request.Form);
                    }
                }
            }
    2、[FormCollectionBinder]
        public class FormCollection{}
      

  2.   

    怎么用不到
    Controller.UpdateModel 
      

  3.   

    /* ****************************************************************************
     *
     * Copyright (c) Microsoft Corporation. All rights reserved.
     *
     * This software is subject to the Microsoft Public License (Ms-PL). 
     * A copy of the license can be found in the license.htm file included 
     * in this distribution.
     *
     * You must not remove this notice, or any other, from this software.
     *
     * ***************************************************************************/namespace System.Web.Mvc {
        using System;
        using System.Collections;
        using System.Collections.Generic;
        using System.Collections.Specialized;
        using System.Diagnostics.CodeAnalysis;
        using System.Globalization;
        using System.Web.Routing;    public class ValueProviderDictionary : IDictionary<string, ValueProviderResult> {        private readonly Dictionary<string, ValueProviderResult> _dictionary = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);        public ValueProviderDictionary(ControllerContext controllerContext) {
                ControllerContext = controllerContext;
                if (controllerContext != null) {
                    PopulateDictionary();
                }
            }        public ControllerContext ControllerContext {
                get;
                private set;
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public int Count {
                get {
                    return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Count;
                }
            }        internal Dictionary<string, ValueProviderResult> Dictionary {
                get {
                    return _dictionary;
                }
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public bool IsReadOnly {
                get {
                    return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).IsReadOnly;
                }
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public ICollection<string> Keys {
                get {
                    return Dictionary.Keys;
                }
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public ValueProviderResult this[string key] {
                get {
                    ValueProviderResult result;
                    Dictionary.TryGetValue(key, out result);
                    return result;
                }
                set {
                    Dictionary[key] = value;
                }
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public ICollection<ValueProviderResult> Values {
                get {
                    return Dictionary.Values;
                }
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public void Add(KeyValuePair<string, ValueProviderResult> item) {
                ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Add(item);
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public void Add(string key, ValueProviderResult value) {
                Dictionary.Add(key, value);
            }        private void AddToDictionaryIfNotPresent(string key, ValueProviderResult result) {
                if (!String.IsNullOrEmpty(key)) {
                    if (!Dictionary.ContainsKey(key)) {
                        Dictionary.Add(key, result);
                    }
                }
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public void Clear() {
                ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Clear();
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public bool Contains(KeyValuePair<string, ValueProviderResult> item) {
                return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Contains(item);
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public bool ContainsKey(string key) {
                return Dictionary.ContainsKey(key);
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public void CopyTo(KeyValuePair<string, ValueProviderResult>[] array, int arrayIndex) {
                ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).CopyTo(array, arrayIndex);
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public IEnumerator<KeyValuePair<string, ValueProviderResult>> GetEnumerator() {
                return ((IEnumerable<KeyValuePair<string, ValueProviderResult>>)Dictionary).GetEnumerator();
            }        private void PopulateDictionary() {
                CultureInfo currentCulture = CultureInfo.CurrentCulture;
                CultureInfo invariantCulture = CultureInfo.InvariantCulture;            // We use this order of precedence to populate the dictionary:
                // 1. Request form submission (should be culture-aware)
                // 2. Values from the RouteData (could be from the typed-in URL or from the route's default values)
                // 3. URI query string            NameValueCollection form = ControllerContext.HttpContext.Request.Form;
                if (form != null) {
                    string[] keys = form.AllKeys;
                    foreach (string key in keys) {
                        string[] rawValue = form.GetValues(key);
                        string attemptedValue = form[key];
                        ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, currentCulture);
                        AddToDictionaryIfNotPresent(key, result);
                    }
                }            RouteValueDictionary routeValues = ControllerContext.RouteData.Values;
                if (routeValues != null) {
                    foreach (var kvp in routeValues) {
                        string key = kvp.Key;
                        object rawValue = kvp.Value;
                        string attemptedValue = Convert.ToString(rawValue, invariantCulture);
                        ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, invariantCulture);
                        AddToDictionaryIfNotPresent(key, result);
                    }
                }            NameValueCollection queryString = ControllerContext.HttpContext.Request.QueryString;
                if (queryString != null) {
                    string[] keys = queryString.AllKeys;
                    foreach (string key in keys) {
                        string[] rawValue = queryString.GetValues(key);
                        string attemptedValue = queryString[key];
                        ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, invariantCulture);
                        AddToDictionaryIfNotPresent(key, result);
                    }
                }
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public bool Remove(KeyValuePair<string, ValueProviderResult> item) {
                return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Remove(item);
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public bool Remove(string key) {
                return Dictionary.Remove(key);
            }        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            public bool TryGetValue(string key, out ValueProviderResult value) {
                return Dictionary.TryGetValue(key, out value);
            }        #region IEnumerable Members
            [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
            IEnumerator IEnumerable.GetEnumerator() {
                return ((IEnumerable)Dictionary).GetEnumerator();
            }
            #endregion    }
    }用到了它的父类 NameValueCollection 
      

  4.   

      NameValueCollection form = ControllerContext.HttpContext.Request.Form;
    ControllerContext.HttpContext.Request.Form类型是 NameValueCollection 不是 FormCollection
      

  5.   


    FormCollection通常用作由[HttpPost]装饰的action方法的形参