将视图模型传递给辅助类
我想使用辅助类创建 html 在我的测试应用程序中显示标签云。
我可以在部分视图中使用 foreach 循环来访问模型中的每个项目
Inherits="System.Web.Mvc.ViewUserControl < IEnumerable < MyTestproject.Models.TagCount > >
foreach (var item in Model) {
}
但是当我尝试将模型传递给 Helper 类并使用 foreach 循环时,我收到以下错误:
public static string DisplayCloud < TagCount >(TagCount objTags) {
..
foreach (var item in objTags) {
}
}
foreach 语句无法对类型的变量进行操作'TagCount' 因为 'TagCount' 不包含 'GetEnumerator' 的公共定义
有什么区别或者我是否错误地传递了它?
I would like to display a tag cloud in my test application using a helper class to create the html.
I can use a for each loop in the partial view to visit each item in the model
Inherits="System.Web.Mvc.ViewUserControl < IEnumerable < MyTestproject.Models.TagCount > >
foreach (var item in Model) {
}
But when I try to pass the model to the Helper class and use a for each loop I receive the following error:
public static string DisplayCloud < TagCount >(TagCount objTags) {
..
foreach (var item in objTags) {
}
}
foreach statement cannot operate on variables of type 'TagCount' because 'TagCount' does not contain a public definition for 'GetEnumerator'
What is the difference or am I passing it incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为你正在传递一种不同的类型。
视图正在获取
IEnumerable
帮助程序正在获取
TagCount
您的帮助程序代码需要是:
方法上的泛型类型似乎无用/非法,因为它是实际类型,所以我删除了它,并修复了参数类型。
Because you're passing a different type.
The view is getting
IEnumerable<TagCount>
The helper is getting
TagCount
Your helper code needs to be:
The generic type on the method seems useless/illegal, since it's an actual type, so I removed it, as well as fixing the argument type.
更仔细地查看视图的类签名和辅助方法的签名之间的区别:
该方法需要接收一个
IEnumerable< ;TagCount>
以便调用 foreach。Look more closely at the difference between your view's class signature and your helper method's signature:
The method needs to receive an
IEnumerable<TagCount>
in order to call foreach.