在代码块(razor视图)中动态生成样式

时间:2014-09-18 11:17:22

标签: c#

我创建了一个视图调用styles.cshtml,我想动态创建样式表。  什么是css内部代码块的正确形式?  下面是错误的代码块,这个例子的正确形式是什么。

 @foreach (var column in Model.Tabs[i].MegaMenuColumns) //columns
 {
        if(column.Width || column.TextColor || column.BackgroundColor)
        {
            #column@(column.Id)
            {
                width: @(column.Width) ;
                color: @(column.TextColor);
                if(column.IsGradient)
                {
                    background-image: -o-linear-gradient(bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                    background-image: -moz-linear-gradient(bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                    background-image: -webkit-linear-gradient(bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                    background-image: -ms-linear-gradient(bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                    background-image: linear-gradient(to bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                }
                else
                {
                    background: @(column.BackgroundColor);
                }
            }
        }
}

1 个答案:

答案 0 :(得分:0)

你必须告诉那里从纯文本的开始和形式开始剃刀代码。

@:告诉它是纯文本而不是razor代码,并且@@(some code here) telll它是c#代码块:

@foreach (var column in Model.Tabs[i].MegaMenuColumns)
 {
        if(column.Width || column.TextColor || column.BackgroundColor)
        {
            @:#column@(column.Id)
            @:{
                @:width: @(column.Width) ;
                @:color: @(column.TextColor);
                if(column.IsGradient)
                {
                    @:background-image: -o-linear-gradient(bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                    @:background-image: -moz-linear-gradient(bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                    @:background-image: -webkit-linear-gradient(bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                    @:background-image: -ms-linear-gradient(bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                    @:background-image: linear-gradient(to bottom, @(column.BackgroundColor) 0%, @(column.BackgroundColorGradient) 100%);
                }
                else
                {
                    @:background: @(column.BackgroundColor);
                }
            @:}
        }
}