我想在 C# 块代码之间使用 html 标签,我已经尝试过了,但我遇到了错误。如何在 cshtml 文件中的 C# 块代码之间插入 html 标签?
错误是:
只有赋值调用自增自减和新对象表达式可以作为语句使用
@{
Func<List<Comment>, Comment, HelperResult> ShowTree = null;
ShowTree = (items, node) =>
{
@<text>
<ul>
@foreach (var comment in items)
{
}
</ul>
</text>;
return null;
};
}
更新2:
我用过@:
但还是有问题。那是;expected
我怎么解决这个问题?
@{
Func<List<Comment>, Comment, int, HelperResult> ShowTree = null;
ShowTree = (items, parentItem, parentId) =>
{
var count = items.Count(p => p.ParentId == parentId);
if (count > 0)
{
@:<ul>
foreach (var item in items.Where(p => p.ParentId ==
parentId).ToList())
{
@:<li>
string collapseId = string.Format("collapse_{0}", item.Id);
@:<a class="btn btn-link" data-toggle="collapse"
href="#@{collapseId}" aria-expanded="True" aria-controls="@{collapseId}">
@:<span class="fa fa-minus" aria-hidden="true">
Commenter Name</span>
@:</a>
@:<div class="collapse show" id="@{collapseId}">
@:<div class="card">
@:<p>item.Description</p>
@:</div>
ShowTree(items, item, item.Id);
@:</div>
@:</li>
}
@:</ul>
}
return null;
};
};
更新 3:浏览器中带有错误消息的最新代码
@using Jahan.Beta.Web.App.Helper
@using Jahan.Beta.Web.App.Models
@using Microsoft.AspNetCore.Mvc.Razor
@using Jahan.Beta.Web.App.Helper
@model List<Comment>
@{
Func<List<Comment>, Comment, int, HelperResult> ShowTree = null;
ShowTree = (items, parentItem, parentId) =>
{
var count = items.Count(p => p.ParentId == parentId);
if (count > 0)
{
@:<ul>
@<text>@{
foreach (var item in items.Where(p => p.ParentId ==
parentId).ToList())
{
string collapseId = string.Format("collapse_{0}", item.Id);
<li>
<a class="btn btn-link" data-toggle="collapse" href="@collapseId" aria-expanded="True" aria-controls="@collapseId">
<span class="fa fa-minus" aria-hidden="true"> Commenter Name</span></a>
<div class="collapse show" id="@collapseId"><div class="card"><p>@item.Description</p></div>
@ShowTree(items, item, item.Id);
</div>
</li>
}
}</text>
@:</ul>
}
return null;
};
}
@{
Func<List<Comment>, HelperResult> CreateTree = null;
CreateTree = commentList => new Func<List<Comment>, HelperResult>(
@<text>@{
List<Comment> nodesRoot = commentList.Where(p => p.ParentId ==
null).ToList();
<ul>
@foreach (var comment in nodesRoot)
{
<li>
<p>@comment.Description</p>
@ShowTree(commentList, comment, comment.Id);
</li>
}
</ul>
}
</text>)(null);
}
<div class="media mb-4">
<div class="media-body">
@CreateTree(Model)
</div>
</div>
编译处理此请求所需的资源时出错。请查看以下特定错误详细信息并适当修改您的源代码。生成的代码
; 预期的