【问题标题】:Inserting html tags between C# block code in cshtml file在cshtml文件中的C#块代码之间插入html标签
【发布时间】:2018-01-12 18:51:32
【问题描述】:

我想在 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>

编译所需资源时出错 处理这个请求。请查看以下具体错误 详细信息并适当地修改您的源代码。 生成代码

;预计

【问题讨论】:

    标签: razor asp.net-core asp.net-core-mvc asp.net-core-2.0 razor-pages


    【解决方案1】:

    对于 cshtml 中的 C# 代码块的内联转义,请使用 @:。例如:

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

    【讨论】:

    • 您的答案似乎接近正确答案,但仍然存在问题!请查看新的更新 2。
    • @Jahan 为什么在 C# 代码块中使用新的 C# 代码块?使用普通的@。例如:@:&lt;a class="btn btn-link" data-toggle="collapse" href="#@collapseId" aria-expanded="True" aria-controls="@collapseId"&gt;
    • 现在,我使用了普通的@。但是第 15、18、26、29 和 31 行仍然存在问题。
    • @Jahan 错误行提示断线有问题。使用内联转义 (@:) 时,您需要在一行中完成所有操作。例如试试这个:pastebin.com/raw/mQxQ5V50
    • @Jahan 我检查了两次正常视图(cshtml:pastebin.com/raw/HkTz1aHm,结果:image.ibb.co/gE3hZ6/dontbefoolidiot.png)和我的工作(VS15.5 + .Net Core 2.0.5)。最后——下次不要用!!,不正常
    【解决方案2】:

    正如我所见,您希望通过将 Html 与 C# 代码混合来呈现一些内容,而不是将 C# 代码插入到 cshtml 文件中,您可以创建自定义 Html 帮助程序,它将是这样的:

         public static class CustomHtmlHelper
            {
                public static IHtmlContent ShowTree(this IHtmlHelper htmlHelper)
                {
                    Func<List<Comment>, Comment, HelperResult> ShowTree = null;
    
                    var builder = new StringBuilder();
    
                    ShowTree = (items, node) =>
                    {
                        builder.Append("<ul>");
    
                        foreach (var comment in items)
                        {
                            builder.Append("<li>"+comment.Content+"</li>");
    
                        }
                        builder.Append("</ul>");
                        return null;
                    };
    
                    //Just for testing purposes only
    
                    var comments = new List<Comment> { new Comment { Content = "Comment 1" }, new Comment { Content = "Comment 2" } };
    
                    ShowTree(comments, new Comment());
    
                    //
                    return new HtmlString(builder.ToString());
    
                }
            }
    
    • 在cshtml中

      @Html.ShowTree()

    【讨论】:

    • 我只想在cshtml文件中实现代码而不创建类。
    • @Jahan:为什么? Ahmed 为您的问题提供了一个优雅的解决方案。
    • @garfbradaz:艾哈迈德的回答是正确的,但我只想在 cshtml 文件中实现。
    【解决方案3】:

    为什么不将&lt;text&gt;&lt;ul&gt; 移到{} 之外。像这样:

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

    用多个括号分隔代码

    @{
        var commentList = new List<string>();
    }
    <text>
        <ul>
            @foreach (var comment in commentList)
            {
                <li>@comment</li>
            }
    
        </ul>
    </text>;
    

    【讨论】:

    • 好的,我认为您应该尝试将您的项目存储在一个变量中的一个 @{} 中,然后使用 @foreach(){} 。所以不要试图把所有东西都放在一个@{}中。请参阅我的编辑以获取简单示例
    • 很遗憾,您的解决方案没有用处。
    【解决方案4】:

    对你有帮助吗?

    @{
    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())
    {
        string collapseId = string.Format("collapse_{0}", item.Id);
                @<text>
                <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;
    };
    }
    

    【讨论】:

    • 请查看更新 3。
    【解决方案5】:

    删除&lt;text&gt;之前的@

    【讨论】:

    • 当我删除 之前的 @ 时,我得到另一个错误:“当前上下文中不存在名称 'text'。无法解析符号 'text'
    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多