C# 序列化请求。表单到字典或其他东西
您好,我需要将Request.Form作为参数传递,但首先我必须向它添加一些键/值对。我得到一个例外,集合是只读的 我试过:C# 序列化请求。表单到字典或其他东西,c#,.net,asp.net,C#,.net,Asp.net,您好,我需要将Request.Form作为参数传递,但首先我必须向它添加一些键/值对。我得到一个例外,集合是只读的 我试过: System.Collections.Specialized.NameValueCollection myform = Request.Form; foreach(KeyValuePair<string, string> pair in Request.Form) { Response.Write(Convert.ToString(pair.Ke
System.Collections.Specialized.NameValueCollection myform = Request.Form;
foreach(KeyValuePair<string, string> pair in Request.Form)
{
Response.Write(Convert.ToString(pair.Key) + " - " + Convert.ToString(pair.Value) + "<br />");
}
我得到了同样的错误
我试过:
System.Collections.Specialized.NameValueCollection myform = Request.Form;
foreach(KeyValuePair<string, string> pair in Request.Form)
{
Response.Write(Convert.ToString(pair.Key) + " - " + Convert.ToString(pair.Value) + "<br />");
}
foreach(Request.Form中的KeyValuePair对)
{
Response.Write(Convert.ToString(pair.Key)+“-”+Convert.ToString(pair.Value)+“
”;
}
测试我是否可以将其逐个传递到另一个字典,但我得到:
System.InvalidCastException:已指定
强制转换无效
有人来帮忙吗?Thanx您不需要将
字符串
强制转换为字符串
NameValueCollection
是围绕字符串键和字符串值构建的。快速扩展方法如何:
public static IDictionary<string, string> ToDictionary(this NameValueCollection col)
{
var dict = new Dictionary<string, string>();
foreach (var key in col.Keys)
{
dict.Add(key, col[key]);
}
return dict;
}
安德烈
那么:
System.Collections.Specialized.NameValueCollection myform = Request.Form;
IDictionary<string, string> myDictionary =
myform.Cast<string>()
.Select(s => new { Key = s, Value = myform[s] })
.ToDictionary(p => p.Key, p => p.Value);
System.Collections.Specialized.NameValueCollection myform=Request.Form;
IDictionary myDictionary=
myform.Cast()
.Select(s=>new{Key=s,Value=myform[s]})
.ToDictionary(p=>p.Key,p=>p.Value);
使用LINQ将所有内容保持在一条“线路”上。这可以扩展为以下扩展方法:
public static IDictionary<string, string> ToDictionary(this NameValueCollection col)
{
IDictionary<string, string> myDictionary = new Dictionary<string, string>();
if (col != null)
{
myDictionary =
col.Cast<string>()
.Select(s => new { Key = s, Value = col[s] })
.ToDictionary(p => p.Key, p => p.Value);
}
return myDictionary;
}
publicstaticidictionary到dictionary(此NameValueCollection列)
{
IDictionary myDictionary=新字典();
如果(列!=null)
{
我的字典=
卡斯特上校()
.Select(s=>new{Key=s,Value=col[s]})
.ToDictionary(p=>p.Key,p=>p.Value);
}
返回myDictionary;
}
希望这有帮助。公共静态IEnumerable到numerable(此NameValueCollection集合)
public static IEnumerable<Tuple<string, string>> ToEnumerable(this NameValueCollection collection)
{
return collection
//.Keys
.Cast<string>()
.Select(key => new Tuple<string, string>(key, collection[key]));
}
{
回收
//.钥匙
.Cast()
.Select(key=>newtuple(key,collection[key]);
}
或
公共静态字典目录(此NameValueCollection集合)
{
回收
//.钥匙
.Cast()
.ToDictionary(key=>key,key=>collection[key]);
}
如果您已经在使用MVC,那么您可以使用0行代码
using System.Web.Mvc;
var dictionary = new Dictionary<string, object>();
Request.Form.CopyTo(dictionary);
使用System.Web.Mvc;
var dictionary=newdictionary();
Request.Form.CopyTo(字典);
thanx Matthew,我明天要试试。我暂时使用隐藏字段。matthew-nice one(和+1)。作为一个苏格兰人,我忍不住在下面用一个小LINQ:)捅了捅。新年快乐-帕尔夫…:-)(顺便说一句,刚刚读了一点你的博客,喜欢regula和javascript linq文章)顺便说一句,你可能想回顾一下这个javascript linq实现嘻嘻,耶-:)。我有时对林克做得太过分了。事实上,我遇到了一个难题,因为我得到了一个新的机会,必须在LAMP堆栈上进行配置,我想知道在没有linq的情况下如何处理php5对象:)(虽然我确实看到了这一点)NameValueCollection需要是NameValueCollection