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

谁说胖子不能爱 提交于 2019-12-04 08:26:25

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]);

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);
            }
    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);
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!