Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor syntax in <style>

what is the correct syntax for using Razor syntax inside a style tag of a view? I tried two different ways:

<style scoped>
    .test1
    {
        background-color: @Model.bgColor;
    }
    .test2
    {
        background-color: @(Model.bgColor);
    }
</style>

Both versions will break syntax highlighting and code indentation of Visual Studio 2012 (Version 11.0.60610.01 Update 3).

Any idea? Thanks!

like image 329
Ingmar Avatar asked Oct 21 '13 12:10

Ingmar


1 Answers

Try this:

@{
    var mystyle = string.Concat("<style scoped> .test1 { background-color: ", Model.bgColor, "; } .test2 { background-color: ", Model.bgColor, "; } </style>");
}

@MvcHtmlString.Create(mystyle)

Edit

@IngmarBobe sorry, but I tested with these two examples on the same version of VS2012 and working properly.

<style scoped>
    .test1
    {
        background-color: @Model.BgColor;
    }
    .test2
    {
        background-color: @(Model.BgColor);
    }
</style>

and

@{
<style scoped>
    .test1
    {
        background-color: @Model.BgColor;
    }
    .test2
    {
        background-color: @(Model.BgColor);
    }
</style>
}

What type of data is "BgColor"?. In my tests "BgColor" is defined in this manner and it works well:

public string BgColor { get { return "#611546"; } }
like image 185
andres descalzo Avatar answered Sep 25 '22 03:09

andres descalzo