104 lines
3.0 KiB
C#
104 lines
3.0 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using Microsoft.AspNet.Http;
|
|
|
|
namespace Microsoft.AspNet.Mvc.ModelBinding
|
|
{
|
|
/// <summary>
|
|
/// An <see cref="IValueProvider"/> adapter for data stored in an <see cref="IFormCollection"/>.
|
|
/// </summary>
|
|
public class FormValueProvider : BindingSourceValueProvider, IEnumerableValueProvider
|
|
{
|
|
private readonly CultureInfo _culture;
|
|
private PrefixContainer _prefixContainer;
|
|
private IFormCollection _values;
|
|
|
|
/// <summary>
|
|
/// Creates a value provider for <see cref="IFormCollection"/>.
|
|
/// </summary>
|
|
/// <param name="bindingSource">The <see cref="BindingSource"/> for the data.</param>
|
|
/// <param name="values">The key value pairs to wrap.</param>
|
|
/// <param name="culture">The culture to return with ValueProviderResult instances.</param>
|
|
public FormValueProvider(
|
|
BindingSource bindingSource,
|
|
IFormCollection values,
|
|
CultureInfo culture)
|
|
: base(bindingSource)
|
|
{
|
|
if (bindingSource == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(bindingSource));
|
|
}
|
|
|
|
if (values == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(values));
|
|
}
|
|
|
|
_values = values;
|
|
_culture = culture;
|
|
}
|
|
|
|
public CultureInfo Culture
|
|
{
|
|
get
|
|
{
|
|
return _culture;
|
|
}
|
|
}
|
|
|
|
protected PrefixContainer PrefixContainer
|
|
{
|
|
get
|
|
{
|
|
if (_prefixContainer == null)
|
|
{
|
|
_prefixContainer = new PrefixContainer(_values.Keys);
|
|
}
|
|
|
|
return _prefixContainer;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override bool ContainsPrefix(string prefix)
|
|
{
|
|
return PrefixContainer.ContainsPrefix(prefix);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual IDictionary<string, string> GetKeysFromPrefix(string prefix)
|
|
{
|
|
if (prefix == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(prefix));
|
|
}
|
|
|
|
return PrefixContainer.GetKeysFromPrefix(prefix);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override ValueProviderResult GetValue(string key)
|
|
{
|
|
if (key == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(key));
|
|
}
|
|
|
|
var values = _values[key];
|
|
if (values.Count == 0)
|
|
{
|
|
return ValueProviderResult.None;
|
|
}
|
|
else
|
|
{
|
|
return new ValueProviderResult(values, _culture);
|
|
}
|
|
}
|
|
}
|
|
}
|