I´m developing web application in ASP .NET MVC 5 and I need to have some button there which will change style (another .css file) in layout. I´ve tried jQuery + COOKIES solution - it works but there is still other style visible for few moment when page is loading.
Is there any other (better) solution for this problem in MVC 5? It´s not necesary to have COOKIES (when user returns default style can load). After click on some button all pages will use second style and after another click default style will be in use again. Maybe by using some route attribute or something like that.
Many thanks for every advice!
This is just one way - depends on how extensive (or not) the changes are: leverage sections
You'll typically have in boilerplate that shows @section
that are "placeholders" for anything you want to "inject" from View
.
Example:
Views/Shared/_Layout.cshtml
define an optional Section
in <head />
<title>@ViewBag.Title</title>
<!--this could be your "base" style-->
@Styles.Render("~/Content/css")
<!--this could be your "override" style-->
@RenderSection("OverrideStyles", false)
in a View
or _ViewStart
(so you can style multiple views in one place) use it as needed
@section OverrideStyles
{
@if (DateTime.Now.Second % 2 == 0) //Some expression or some value from controller....
{
<style>
div.jumbotron h1 {
color: #ff0000;
}
</style>
}
else
{
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/stackoverflow/all.css?v=693004a3f56e">
}
}
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more »</a></p>
</div>
You can expand from there - even dynamically set entire _Layout
if necessary..
e.g. _ViewStart.cshtml
@{
if (DateTime.Now.Second % 2 == 0)
{
Layout = "~/Views/Shared/_Layout.cshtml";
}
else
{
Layout = "~/Views/Shared/_Layout-bar.cshtml";
}
}
Hth...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With