7

I want to use html tags between C# block code, I've tried it but I got errors. How can I insert html tags between C# block code in cshtml file?

The error is:

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

Update 2: I've used @: but still there is a problem. that is ;expected

How can I solve this problem?

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

Update 3: Latest codes with error messages in browser

@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

5 Answers 5

8

For inline escape from C# codeblock in cshtml use @:. For example:

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

                }
        @:</ul>
        return null;
    };
}
Sign up to request clarification or add additional context in comments.

7 Comments

It seems your answer is near to right answer but still there is a problem! Please look at the new Update 2.
@Jahan Why you are using new C# codeblock in C# codeblock? Use normal @. For example: @:<a class="btn btn-link" data-toggle="collapse" href="#@collapseId" aria-expanded="True" aria-controls="@collapseId">
Now, I've used normal @. but still there are problems in lines 15, 18, 26, 29 and 31.
@Jahan Error lines suggest problem with break lines. When using inline escape (@:) you need to do all in one line. For example try this: pastebin.com/raw/mQxQ5V50
@Jahan I checked twice as normal view (cshtml: pastebin.com/raw/HkTz1aHm, result: image.ibb.co/gE3hZ6/dontbefoolidiot.png) and for me working (VS15.5 + .Net Core 2.0.5). At the end - don't use !! next time, it's not normal
|
2

As I see you want to render some content by mixing Html with C# code, instead of inserting your C# code in cshtml file you can create custom Html helper, it will be something like this:

     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());

            }
        }
  • In cshtml

    @Html.ShowTree()

3 Comments

I want to implement the code just only in cshtml file without creating class.
@Jahan: Why though? Ahmed has given you a elegant solution to your problem.
@garfbradaz: The Ahmad's answer is correct but I want just an implementation in cshtml file.
0

Why don't you move the <text> and <ul> outside the {}. Like this :

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

            foreach (var comment in items)
            {
            }
            return null;
        };

    }
</ul>
</text>;

Separe your code in multiple bracket

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

    </ul>
</text>;

2 Comments

Ok I think you should try to stock your items in a variable in one @{} and after that use the @foreach(){} . So don't try to put everything in one @{}. see my edit for simple example
Unfortunately, your solution that was not useful.
0

Can it help you?

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

1 Comment

Please look at the Update 3.
-1

Remove the @ before <text>.

1 Comment

When I remove the @ before <text> I get another errors: "The name 'text' does not exist in the current context. Cannot resolve symbol 'text'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.