“不包含'GetEnumerator'的公共定义” [英] "does not contain a public definition for 'GetEnumerator"

查看:1314
本文介绍了“不包含'GetEnumerator'的公共定义”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我对IEnumerator有点困惑。它是否准确描述了List< class1>?



错误出现在 foreach 语句中,只有foreach是红色下划线而不是项目或模型。



它说它......不包含'GetEnumerator'的公开定义......'



Index.cshtml

I am a little confused about the IEnumerator in this case. Does it accurately depict List<class1>?

The error is in the "foreach" statement only the "foreach" is red underlined not the "item" or the "Model".

It says it "...does not contain a public definition for 'GetEnumerator...'

Index.cshtml

@model IEnumerator<SimpleMVC5.Models.Class1>
        <table>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.idt</td>
                    <td>@item.datetime</td>
                    <td>@item.col1</td>
                    <td>@item.col2</td>
                    <td>@item.col3</td>
                </tr>
            }
        </table>



HomeController.cs


HomeController.cs

public class HomeController : Controller
    {
        private testContext db = new testContext();
        List<Class1> lst = new List<Class1>();
        public ActionResult Index()
        {
            lst = db.Data1.ToList();
            return View(lst);
        }



Class1.cs


Class1.cs

public class Class1
    {
        public int idt { get; set; }
        public string datetime0 { get; set; }
        public string col1 { get; set; }
        public string col2 { get; set; }
        public string col3 { get; set; }
    }
    public class testContext : DbContext
    {
        public DbSet<Class1> Data1 { get; set; }
    }

推荐答案

它清楚地告诉你对象 Model 不能用作 foreach 中使用的一组对象。这很简单:此类对象的类型应支持 System.Collections.IEnumerable

https://msdn.microsoft.com/en-us/library/system.collections.ienumerable%28v= vs.110%29.aspx [ ^ ]。



注意: IEnumerable not IEnumerator 您需要实现枚举器的IEnumerator 的对象返回 System.Collections.IEnumerable.GetEnumerator



是的,集合通常实现 System.Collections.IEnumerable 。如有疑问,请查看MSDN帮助页面,如果有问题的类型并检查它;始终显示此详细信息。您没有显示 Model 对象的类型,但它肯定不是集合,并且不是从任何集合类型派生的,但可能其中一个成员是集合类型因此,在大多数情况下,您可能仍然不需要实现此界面。



-SA
It clearly tells you that the object Model cannot be used as a set of object for using in foreach. This is simple: the type of such object should support System.Collections.IEnumerable:
https://msdn.microsoft.com/en-us/library/system.collections.ienumerable%28v=vs.110%29.aspx[^].

Pay attention: IEnumerable, not IEnumerator! You would need to implement IEnumerator by the enumerator, the object returned by System.Collections.IEnumerable.GetEnumerator.

Yes, collections generally implement System.Collections.IEnumerable. When in doubt, look at the MSDN help page if the type in question and check it up; this detail is always shown. You did not show the type of your Model object, but it is certainly not a collection and is not derived from any collection type, but probably one of its members is of collection type, so, as in most cases, you probably still won't need to implement this interface.

—SA


这篇关于“不包含'GetEnumerator'的公共定义”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆