CSS پویا در ASP.NET MVC
نویسنده: ناصر طاهری
تاریخ: ۱۳۹۲/۰۹/۱۲ ۲۲:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<head>
@*سایر شیوه نامهها و اسکریپت ها*@
@RenderSection("styles", required: false)
</head> namespace DynamicCssExample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public string GetStyle()
{
Response.ContentType = "text/css";
//در این قسمت میتوانیم به دیتابیس متصل شویم و شیوه نامهی مورد نظر را واکشی کنیم و بازگشت دهیم
return "h2{color:yellow}";
}
}
} @{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section styles
{
<link rel="stylesheet" href="@Url.Action("GetStyle", "Home")" type="text/css"/>
}
<h2>Index</h2> public class CSS
{
public string Color { set; get; }
} public ActionResult GetDynamicStyle()
{
var color = "White";
if (DateTime.Now.Hour > 18 || DateTime.Now.Hour < 8)
{
color = "Black";
}
this.Response.ContentType = "text/css";
return PartialView(viewName: "~/Views/Home/_CSS.cshtml", model: new CSS { Color = color });
} @model DynamicMvcCSS.Controllers.CSS
.foo {
color: @Model.Color;
}