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>
"); }
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With