<style>中的Razor语法

Ing*_*mar 7 asp.net-mvc razor visual-studio-2012

在视图的样式标记中使用Razor语法的正确语法是什么?我尝试了两种不同的方式:

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

这两个版本都将破坏Visual Studio 2012(版本11.0.60610.01更新3)的语法突出显示和代码缩进.

任何的想法?谢谢!

and*_*lzo 5

试试这个:

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

@MvcHtmlString.Create(mystyle)
Run Code Online (Sandbox Code Playgroud)

编辑

@IngmarBobe对不起,但我在同一版本的VS2012上测试了这两个例子,并且工作正常.

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

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

什么类型的数据是"BgColor"?在我的测试中,"BgColor"以这种方式定义并且运行良好:

public string BgColor { get { return "#611546"; } }
Run Code Online (Sandbox Code Playgroud)