Asp.net mvc 为具有从数据库获取数据的属性的元素设置css属性值

Asp.net mvc 为具有从数据库获取数据的属性的元素设置css属性值,asp.net-mvc,css,razor,Asp.net Mvc,Css,Razor,当属性值来自模型中的属性时,如何设置css属性或属性 例如,我想更改车身背景色,背景色的值为@Model.BgColor。@model.BgColor从sql数据库获取数据 我不知道如何从css中的模型中获取BgColor属性。当我在_Layout中尝试此设置时,我得到一个错误,对象引用未设置为对象的实例: @model ItbTid.Models.DynCss <!DOCTYPE html> <html lang="en"> <head>

当属性值来自模型中的属性时,如何设置css属性或属性

例如,我想更改车身背景色,背景色的值为@Model.BgColor。@model.BgColor从sql数据库获取数据

我不知道如何从css中的模型中获取BgColor属性。当我在_Layout中尝试此设置时,我得到一个错误,对象引用未设置为对象的实例:

@model ItbTid.Models.DynCss
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    <style>
       body{
            background-color:@Model.BgColor;
           }
    </style>
</head>
@model ItbTid.Models.DynCss
@Title-我的ASP.NET MVC应用程序
@style.Render(“~/Content/css”)
@Scripts.Render(“~/bundles/modernizer”)
身体{
背景色:@Model.BgColor;
}
总结:
如何为一个元素设置css属性值,该元素的属性位于从数据库获取数据的DynCss模型中

如何实例化模型?你能把代码贴出来吗?我很想知道这是否可行!你解决过这个问题吗?