0

I want to change the theme of my web page on user selection I am using MVC3. My idea is to include various style sheets inside the Content folder and let the _layout.cshtml file decide which css file to call on user selection At present all i can do is include a single tag which is reflected on all the pages I want the view to be the same but the style sheets should change on selection.

I tried this code inside _Layout.cshtml:

    <script type="text/javascript">
    function loadjscssfile(filename) {
        var fileref = document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)   

Inside Body tag:

<select>
        <option onclick="loadjscssfile("@Url.Content("~/Content/Site1.css")")">Theme1</option>
        <option onclick="loadjscssfile("@Url.Content("~/Content/Site2.css")")">Theme2</option>
        </select>  

But this also does not work:( Please help me.I am new to MVC3...

2 Answers 2

1

This post should help you:

ASP.NET MVC Theme Supported Razor View Engine

UPDATE: This sample won't compile with mvc3. You should do some "find and replace" for

ViewModel.Title -> ViewBag.Title
ViewModel.Message -> ViewBag.Message
ViewModel.PasswordLength -> ViewBag.PasswordLength

return new ViewEngineResult(CreateView(controllerContext, viewPath, masterPath), this, incompletMatch); -> return new ViewEngineResult(CreateView(controllerContext, viewPath, masterPath), this);
Sign up to request clarification or add additional context in comments.

8 Comments

hi,i have already gone through this link and also downloaded the entire zip file but once i run it is giving me error as:ViewModel does not exists in the current context and i have no idea about ViewModels
If you need more powerful themming you can also try my CavemanTools Mvc toolkit (nuget it). See how to use themes here bitbucket.org/sapiensworks/caveman-tools/wiki/Themes
Updated answer. After the edit compile and works on my machine with MVC3
@Iridio Hi All errors are resolved except one: Inside FindPartialView() return new ViewEngineResult(CreateView(controllerContext, viewPath, masterPath), this);
if you change to return new ViewEngineResult(CreateView(controllerContext, viewPath, masterPath), this); in line 96 and 125 of ThemeableVirtualPathProviderViewEngine.cs, it should works. At least works for me
|
0

I think it is better if you keep one stylesheet and only changes a class on your body tag

<body class="red|blue">
    //your content
</body>

and inside your style sheet you only add different styles to each selector

.red{
    background-color: red;
}

.blue{
    background-color: blue;
}

1 Comment

Thanx for the reply But how should i change the Color dynamically whethr i should include some switch logic inside the controller?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.