7
votes

The @Helpers is gone from Razor in ASP.NET Core 3.1 and now as an alternative suggested to use Razor code blocks. https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-3.1#razor-code-blocks

Unfortunately, if I add example code

@{
    void RenderName(string name)
    {
    <p>Name: <strong>@name</strong></p>
    }
}

to Razor page(cshtml) I get error form Intelisence cannot resolve value, it shows that HTML is not accepted in that place and c# code expected. How to use Razor code blocks?

enter image description here

6
Same here, even if I add a simple div. Did you find a solution for this?Daniel Z.
btw - it renders correctly, so it seems to really be an editor/intellisense thing.Daniel Z.
posted the answer. ran into the exactly same thing even opened an issue.. included in answerSeabizkit

6 Answers

0
votes

Have you tried Html.Raw($"<p>Name: <strong>{@name}</strong></p>") ?

Documentation

0
votes

I see that HTML code is expected as Razor (C#) code. That's the problem.

Try to use:

<text>
<p>Name: <strong>@name</strong></p>
</text>

Or alternatively:

@:<p>Name: <strong>@name</strong></p>

<text> is useful when you have blocks, but if you only use one line you can use "@:"

0
votes

In my case the problem was Resharper. Updating didnt solve the problem (version 2020.2.4). Disabled it now via "Extensions" -> "Manage Extensions". Took me way too long to find the cause.

Update:

Reactivated Resharper and disabled code inspection for .cshtml-files via. "Extensions" -> "Resharper" -> "Options" -> "Code Inspection" -> "Ignored Code" -> "File masks" -> "Add" -> "*.cshtml".

0
votes

If I got it right then maybe this is what you need

@{
       @:<p>Name: <strong>@Model.Email</strong></p>
 }
0
votes

This issue have been resolved I believe, event intellisense is working fin on VS 2019 (v 16.7.5) it is working as expected.

Index.cshtml

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    @RenderDivision()
</div>

@{ 
    Microsoft.AspNetCore.Html.IHtmlContent RenderDivision()
    {
        return Html.Raw("<div>This is my div tag!</div>");
    }
}

renders:

enter image description here

0
votes

https://github.com/dotnet/AspNetCore.Docs/issues/18543#event-3384859964

key word functions, makes it work... how odd

Ahh, interesting what you're seeing then is a C# restriction.When you say @functions {...} what you're saying is add the following to my class body where @{} entails add the following to a method in my class body (a local function). Therefore since C# doesn't support overloaded local functions you can't utilize them in @{}.

@functions 
{
     void RenderName(string name)
    {
        <p>Name: <strong>@name</strong></p>
    }
}