错误:变量' int'不包含' GetEnumerator'的公开定义ASP.NET MVC

时间:2017-01-19 19:59:01

标签: asp.net asp.net-mvc

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

  @{       
  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语句无法对类型&#39; int&#39;的变量进行操作。因为&#39; int&#39;不包含&#39; GetEnumerator&#39;

的公开定义



怎么能修好?

2 个答案:

答案 0 :(得分: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 />
                }

答案 1 :(得分:1)

试试这个..

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