Andreas
Andreas

Reputation: 250

FormCollection to expandoObject

Is there a way I can copy a FormCollection form into an ExpandoObject?

I will get a post from a third party company. Others third party will use get and they all end up in a method that takes a dynamic input parameter.

Thanks!

Upvotes: 2

Views: 871

Answers (1)

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15148

Well, not the most elegant code (probably better ways to do it), but one way could be something like this:

[HttpPost]
public ActionResult Test(FormCollection collection)
{
    dynamic expando = new ExpandoObject();
    var dictionary = (IDictionary<string, object>) expando;

    foreach (var item in collection.AllKeys.ToDictionary(key => key, value => collection[value]))
    {
        dictionary.Add(item.Key, item.Value);
    }
    // your expando will be populated here ...
    // do awesomeness
}

I hope this helps (at least it might give you an idea).

Upvotes: 4

Related Questions