Effy Sille
Effy Sille

Reputation: 189

Error: Variable 'int' does not contain a public definition for 'GetEnumerator' ASP.NET MVC

I would like to show all the files connected to the comment. There are models of Files and Comments.

  @{       
  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>
        }
    }

This line

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

throws an error: foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator'.



How could this be fixed?

Upvotes: 1

Views: 1070

Answers (2)

Muhammad Ramzan
Muhammad Ramzan

Reputation: 288

try this..

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

Upvotes: 0

Yashveer Singh
Yashveer Singh

Reputation: 1977

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 />
                }

Upvotes: 1

Related Questions