如何在 ASP.NET MVC2 中枚举表单集合
我之前使用过 如何在 ASP 中枚举 formcollection .NET MVC? 的实现,但现在我在 VS2010 和 MVC2 上抱怨:
Error 1 Cannot implicitly convert type 'System.Web.Mvc.IValueProvider' to 'System.Collections.Generic.IDictionary'. An explicit conversion exists (are you missing a cast?) C:\~\ProjectMVC\Controllers\TheController.cs line ProjectMVC
代码是......
IDictionary<string, ValueProviderResult> tmpCollection = collection.ToValueProvider();
for (int j = 1; j <= noprops; j++)
{
string shopNmTmp =
(from t in tmpCollection
where t.Key.StartsWith(j + ".discount.sname." + j)
select t.Value.AttemptedValue).First();
string shopCdTmp =
(from t in tmpCollection
where t.Key.StartsWith(j + ".discount.sref." + j)
select t.Value.AttemptedValue).First();
...
当我不看的时候,有什么改变吗?这可以编译、工作和运行,并且在 MVC1 中没有任何问题;但不会在 2 中编译。
谢谢
更新,
我在技术上只是通过使用:
Dictionary<string, string> tmpCollection = collection.AllKeys.ToDictionary(k => k, v => collection[v]);
来解决这个问题。
但我仍然对它为什么在版本之间发生变化感兴趣。
I was previously using How can a formcollection be enumerated in ASP.NET MVC? 's implementation but now I'm on VS2010 and MVC2 its complaining:
Error 1 Cannot implicitly convert type 'System.Web.Mvc.IValueProvider' to 'System.Collections.Generic.IDictionary'. An explicit conversion exists (are you missing a cast?) C:\~\ProjectMVC\Controllers\TheController.cs line ProjectMVC
The code is ...
IDictionary<string, ValueProviderResult> tmpCollection = collection.ToValueProvider();
for (int j = 1; j <= noprops; j++)
{
string shopNmTmp =
(from t in tmpCollection
where t.Key.StartsWith(j + ".discount.sname." + j)
select t.Value.AttemptedValue).First();
string shopCdTmp =
(from t in tmpCollection
where t.Key.StartsWith(j + ".discount.sref." + j)
select t.Value.AttemptedValue).First();
...
Did something change when I wasn't looking; this compiles and works and runs and has no issues in MVC1; but wont compile in 2.
Thanks
Update
I techically just fixed this by using:
Dictionary<string, string> tmpCollection = collection.AllKeys.ToDictionary(k => k, v => collection[v]);
instead.
But I'd still be interested in why it changed between versions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从技术上讲,我只是通过使用以下方法修复了此问题:
在收集后添加换行符。用于格式化
I techically just fixed this by using:
Linebreak added after collection. for formatting