我想显示与评论相关的所有文件。有文件和评论的模型。
@{
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; 。
的公开定义
怎么能修好?
答案 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 />
});