如何在 ASP.NET MVC2 中枚举表单集合

发布于 2024-09-06 07:47:17 字数 1371 浏览 3 评论 0原文

我之前使用过 如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肩上的翅膀 2024-09-13 07:47:17

从技术上讲,我只是通过使用以下方法修复了此问题:

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

在收集后添加换行符。用于格式化

I techically just fixed this by using:

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

Linebreak added after collection. for formatting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文