gpt4 book ai didi

razor - 在cshtml文件中的C# block 代码之间插入html标签

转载 作者:行者123 更新时间:2023-12-01 16:23:29 25 4
gpt4 key购买 nike

我想在 C# block 代码之间使用 html 标签,我已经尝试过,但出现错误。如何在 cshtml 文件中的 C# block 代码之间插入 html 标签?

错误是:

only assignment call increment decrement and new object expressions can be used as a statement

only assignment call increment decrement await and new object expressions can be used as a statement enter image description here

@{
Func<List<Comment>, Comment, HelperResult> ShowTree = null;
ShowTree = (items, node) =>
{
@<text>
<ul>
@foreach (var comment in items)
{

}
</ul>
</text>;
return null;
};

}

Update:When I move the text outside the {}, I encountered some errors, too. Look at this picture. enter image description here

更新 2:我已经使用了 @: 但仍然存在问题。这是;预期

如何解决这个问题?

enter image description here

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

Latest codes

Error messages that shows in browser

An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately. Generated Code

; expected

最佳答案

要从 cshtml 中的 C# 代码块进行内联转义,请使用 @:。例如:

@{
Func<List<Comment>, Comment, HelperResult> ShowTree = null;
ShowTree = (items, node) =>
{
@:<ul>
foreach (var comment in items)
{

}
@:</ul>
return null;
};
}

关于razor - 在cshtml文件中的C# block 代码之间插入html标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48232371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com