1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
16 using System.Collections.Specialized;
\r
17 using System.Diagnostics.CodeAnalysis;
\r
18 using System.Globalization;
\r
19 using System.Web.Mvc.Resources;
\r
21 [SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable",
\r
22 Justification = "It is not anticipated that users will need to serialize this type.")]
\r
23 [SuppressMessage("Microsoft.Design", "CA1035:ICollectionImplementationsHaveStronglyTypedMembers",
\r
24 Justification = "It is not anticipated that users will call FormCollection.CopyTo().")]
\r
25 [FormCollectionBinder]
\r
26 public class FormCollection : NameValueCollection {
\r
28 public FormCollection() {
\r
31 public FormCollection(NameValueCollection collection) {
\r
32 if (collection == null) {
\r
33 throw new ArgumentNullException("collection");
\r
39 public IDictionary<string, ValueProviderResult> ToValueProvider() {
\r
40 CultureInfo currentCulture = CultureInfo.CurrentCulture;
\r
42 Dictionary<string, ValueProviderResult> dict = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);
\r
43 string[] keys = AllKeys;
\r
44 foreach (string key in keys) {
\r
45 string[] rawValue = GetValues(key);
\r
46 string attemptedValue = this[key];
\r
47 ValueProviderResult vpResult = new ValueProviderResult(rawValue, attemptedValue, currentCulture);
\r
48 dict[key] = vpResult;
\r
54 public virtual ValueProviderResult GetValue(string name) {
\r
55 if (String.IsNullOrEmpty(name)) {
\r
56 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
\r
59 string[] rawValue = GetValues(name);
\r
60 if (rawValue == null) {
\r
64 string attemptedValue = this[name];
\r
65 return new ValueProviderResult(rawValue, attemptedValue, CultureInfo.CurrentCulture);
\r
68 private sealed class FormCollectionBinderAttribute : CustomModelBinderAttribute {
\r
70 // since the FormCollectionModelBinder.BindModel() method is thread-safe, we only need to keep
\r
71 // a single instance of the binder around
\r
72 private static readonly FormCollectionModelBinder _binder = new FormCollectionModelBinder();
\r
74 public override IModelBinder GetBinder() {
\r
78 // this class is used for generating a FormCollection object
\r
79 private sealed class FormCollectionModelBinder : IModelBinder {
\r
80 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
\r
81 if (controllerContext == null) {
\r
82 throw new ArgumentNullException("controllerContext");
\r
85 return new FormCollection(controllerContext.HttpContext.Request.Form);
\r