C# 在代码块内动态生成样式(razor视图)

C# 在代码块内动态生成样式(razor视图),c#,C#,我创建了一个视图调用styles.cshtml,我想在其中动态创建样式表。 什么是代码块内css的正确形式? 下面是错误的代码块,该示例的正确形式是什么 @foreach (var column in Model.Tabs[i].MegaMenuColumns) //columns { if(column.Width || column.TextColor || column.BackgroundColor) { #column@(co

我创建了一个视图调用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);
                }
            }
        }
}

您必须从纯文本的起始位置和razor代码的起始位置来判断

@:表示它是纯文本而不是razor代码,并在此处写入@或@某些代码,表示它是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);
                }
            @:}
        }
}
您可以在css周围添加一个标记