ASP.NET MVC2中如何枚举FormCollection?

8
我以前使用的是如何在ASP.NET MVC中枚举formcollection?的实现,但现在我使用VS2010和MVC2时,它会出错:
错误   1   无法隐式将类型'System.Web.Mvc.IValueProvider'转换为
'System.Collections.Generic.IDictionary'。存在显式转换(是否缺少强制转换?) 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]);

相反。

但我仍然对为什么在不同版本之间发生了更改感兴趣。

1个回答

14

我技术上刚刚通过使用以下方式解决了这个问题:

Dictionary<string, string> tmpCollection = collection.
                                 AllKeys.ToDictionary(k => k, v => collection[v]);

为了排版,在集合后添加换行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接