asp.net-mvc get a dictionary in post action or how to transform FormCollection into a dictionary
Asked Answered
E

4

8

anybody knows how to transform the FormCollection into a IDictionary or how to get a IDictionary in the post action ?

Escutcheon answered 4/5, 2010 at 11:29 Comment(0)
F
22

This is just an equivalent of Omnu's code, but it seems more elegant to me:

Dictionary<string, string> form = formCollection.AllKeys.ToDictionary(k => k, v => formCollection[v]);
Flatboat answered 4/5, 2010 at 12:44 Comment(2)
I think it's a matter of style, i personally think Omu's code is a little more verbose, but it's easier to see what happens.Sim
prefer this solution, much neaterToolmaker
E
2

I did it like this:

            var form = new Dictionary<string, string>();
            foreach (var key in formCollection.AllKeys)
            {
                var value = formCollection[key];
                form.Add(key, value);
            }
Escutcheon answered 4/5, 2010 at 12:37 Comment(0)
M
2

On .Net Core this one worked for me.

var collection = Request.Form.Keys.ToDictionary(k => k, v => Request.Form[v].ToString());
Malamud answered 4/2, 2020 at 23:3 Comment(0)
G
0
    public static IDictionary<string, string[]> GetFormParameters(FormCollection collection)
    {
        IDictionary<string, string[]> formParameters = new Dictionary<string, string[]>();
        foreach (var key in collection.AllKeys)
        {
            if (key == null) continue;
            var value = collection.GetValues(key);

            // value = CrossSiteAttackUtil.CleanHtml(value);
            if (value != null)
            {
                formParameters.Add(key, value);
            }
        }
Gish answered 18/7, 2015 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.