Search code examples
c#.netasp.net-mvc-4

Can I add another layout other than the default layout in MVC4?


I am working On MVC4 architecture. I have a default layout i.e _Layout and models and view associated with it. It's working fine.

Now due to some requirement, after I click on specific link I need it to redirect it a whole different layout and there I will associate models and view according to my needs. But how to add a new layout and also keep the existing default layout in place?

For ex: on index page I gave a link <!-- <a href="">Nikhil</a> -->. When I click this I have to go to a separate layout and view.

How can I fix this? I am new to MVC4.


Solution

  • Yes, you can create multiple layout for different views if you want. In my case I have AdminLayout and UserLayout in my Shared folder. Just replace Layout variable.

    Ex.

    // For Admin Pages
    @{
        Layout = "~/Views/Shared/AdminLayout.cshtml";
    }
    
    //For User Pages
    @{
        Layout = "~/Views/Shared/UserLayout.cshtml";
    }
    

    By the way I use razor view engine here.