// Copyright (c) Microsoft Open Technologies, Inc. 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.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Collections;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
///
/// Modelbinder to bind form values to .
///
public class FormCollectionModelBinder : IModelBinder
{
///
public async Task BindModelAsync([NotNull] ModelBindingContext bindingContext)
{
if (bindingContext.ModelType != typeof(IFormCollection) &&
bindingContext.ModelType != typeof(FormCollection))
{
return null;
}
object model = null;
var request = bindingContext.OperationBindingContext.HttpContext.Request;
if (request.HasFormContentType)
{
var form = await request.ReadFormAsync();
if (bindingContext.ModelType.IsAssignableFrom(form.GetType()))
{
model = form;
}
else
{
var formValuesLookup = form.ToDictionary(p => p.Key,
p => p.Value);
model = new FormCollection(formValuesLookup, form.Files);
}
}
else
{
model = new FormCollection(new Dictionary());
}
return new ModelBindingResult(model, bindingContext.ModelName, isModelSet: true);
}
}
}