How do I insert spaces within a razor Code block?

Spaces are ignored when parsing HTML, unless they occur within a pre block. If you want to pad some text, you need to take one of the following approaches:

  1. Wrap it in a block-level HTML element like p or div, and then add padding/margin to the element using CSS. This is the recommended approach.

  2. Use   in place of the regular spaces you're trying to pad with. Only non-breaking spaces are counted when rendering HTML. However, this approach is hacky and not recommended.

  3. Wrap your text in a pre element. Then all whitespace within the <pre> and </pre> tags will be taken into account. However, this approach is also hacky and not recommended.


Why don't you try a different approach. Use a span tag with some padding on it


AndyBuk gave the answer here:
https://forums.asp.net/t/1772048.aspx?How+to+use+no+break+space+HTML+character+inside+if+brackets+in+a+view+

In that link he writes:

The introduction to Razor syntax at:
http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax is quite useful.
To force html output for your string, you may use <text> to Block or @: as a Prefix.

@if (condition)
{
    <text>&nbsp;</text>
    @:&nbsp;
}

<text> &nbsp; &nbsp; &nbsp;</text>

Insert "&nbsp;" to add more blank spaces.