尝试这个:
@{
var mystyle = string.Concat("<style scoped> .test1 { background-color: ", Model.bgColor, "; } .test2 { background-color: ", Model.bgColor, "; } </style>");
}
@MvcHtmlString.Create(mystyle)
编辑
@IngmarBobe 抱歉,但我在同一版本的 VS2012 上使用这两个示例进行了测试,并且工作正常。
<style scoped>
.test1
{
background-color: @Model.BgColor;
}
.test2
{
background-color: @(Model.BgColor);
}
</style>
和
@{
<style scoped>
.test1
{
background-color: @Model.BgColor;
}
.test2
{
background-color: @(Model.BgColor);
}
</style>
}
“BgColor”是什么类型的数据?在我的测试中,“BgColor”以这种方式定义并且效果很好:
public string BgColor { get { return "#611546"; } }