开发者

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

anybody knows how to transform the FormCollection into a IDictionary or how to get a IDict开发者_如何学Cionary in the post action ?


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


On .Net Core this one worked for me.

var collection = Request.Form.Keys.ToDictionary(k => k, v => Request.Form[v].ToString());


    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);
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜