Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for multiline variables

I am new to the Razor engine. What I am trying to accomplish is to set a local variable containing css classes. I would prefer to have this in a more readable format should anyone need to modify this later on.

What is the correct syntax to accomplish this? (I first tried myCSS = "...", then I attempted to use @Html.Raw method)

The only way I can get it to work is to keep everything on one line.

Currently I have:

@{ var myCSS = @Html.Raw("<style>table {border-collapse: collapse; } ...etc</style>"); }

I would like something more readable. Such as:

@{ var myCss = @Html.Raw("
<style>
   table {border-collapse: collapse; } 
   etc....
</style>
"); }
like image 885
John Doe Avatar asked May 28 '26 07:05

John Doe


1 Answers

No need to use Html.Raw because you are defining a C# variable. Html.Raw should be used in the place, where you need to render your CSS.

To preserve the formating you can use the 'verbatim string literal'. It suppress special characters (such as '\' or 'new line') until the next double-quote is encountered.

@{
    var myCss = @"
        <style>
            table {
                border-collapse: collapse;
            }

            etc....
        </style>";
}

@Html.Raw(myCss)

Another question is, why you need CSS in a C# variable ...

like image 95
Lukas Kabrt Avatar answered Jun 01 '26 12:06

Lukas Kabrt