【问题标题】:Error: Variable 'int' does not contain a public definition for 'GetEnumerator' ASP.NET MVC错误:变量“int”不包含“GetEnumerator”ASP.NET MVC 的公共定义
【发布时间】:2017-01-19 19:59:01
【问题描述】:

我想显示所有与评论相关的文件。有文件和评论的模型。

  @{       
  if (Model.Comments != null && Model.Comments.Any())  
  {
            <br/>
            foreach (var comment in Model.Comments)
            {
                @comment.title <br />

                @comment.type <br />
                @comment.content <br />
                @comment.timestamp <br />
                @comment.ApplicationUser <br />
                if (Model.Files.FirstOrDefault().CommentID != null && Model.Files.Any())
                {
                    <br />
                    foreach (var file in Model.Comments.FirstOrDefault().Files.FirstOrDefault().CommentID)
                    {

                        <a href="@file.path">
                            @file.name
                        </a>
                        <br />
                    }
                }
                else
                {
                    <label>No File(s) to Download</label>
                }
                <hr />
            }
        }
        else
        {
            <label>No Comment(s)</label>
        }
    }

这一行

foreach (var file in Model.Comments.FirstOrDefault().Files.FirstOrDefault().CommentID) 

抛出错误:foreach 语句无法对“int”类型的变量进行操作,因为“int”不包含“GetEnumerator”的公共定义。



如何解决这个问题?

【问题讨论】:

  • foreach(Model.Comments.FirstOrDefault().Files.ToList() 中的 var 文件)
  • 你选择了 FirstOrDefault().CommentID 那么为什么 foreach 循环?
  • 因为可以有多个文件
  • 是的,然后使用 Model.Comments.FirstOrDefault().Files.ToList()
  • 试试我的答案,希望它能奏效

标签: asp.net asp.net-mvc


【解决方案1】:
because you want to loop throught the list of file you need to use below .

 foreach (var file in Model.Comments.FirstOrDefault().Files.ToList())
                {

                    <a href="@file.path">
                        @file.name
                    </a>
                    <br />
                }

【讨论】:

    【解决方案2】:

    试试这个..

       $.each(Model.Comments.FirstOrDefault().Files.ToList(), function (i, item) {
                           <a href="@file.path">
                                @item.name
                            </a>
                            <br />
                        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-24
      • 2023-03-16
      • 2018-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      相关资源
      最近更新 更多