Reputation:
I want to use one theme for Admin Panel and one theme is a separate which I want to show my visitors. I mean to say that there should be separate themes for visitor and Administrator of a website. How to do in asp.net mvc? Because in mvc we have only one file _Layout.cshtml and here we have to attach only one theme.
Upvotes: 0
Views: 1537
Reputation: 1074
Define second layout in shared folder with another name like _AdminLayout etc. call the @RenderBody() function in it, Now you can Render layouts via different ways,
1. Define layout in View:
@{
ViewBag.Title = "View_Title";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
2. Rendering layout page from ActionResult (using Controller. View extension method):
public ActionResult Action_Result_Name()
{
return View("Action_Result_Name","_AdminLayout");
}
3. Using _ViewStart.cshtml Page:
_ViewStart.cshtml page used to define the default layout page for the MVC application.
@{
layout = "~/Views/Shared/_AdminLayout.cshtml";
}
If you are using Identity framework then you can define layouts on the basis of conditions as well
@{
if (User.IsInRole("Admin"))
{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
else
{
Layout = "~/Views/Shared/_Layout.cshtml";
}
}
Upvotes: 2
Reputation: 218722
You can create as many layout files as needed. So in your case, it is a good idea to create an admin area which has it's own layout page in which you can use your admin theme css files
Areas are logical grouping of related functionality and files needed for that (controllers,views, styles,code etc).Areas provide a way to partition a large Web app into smaller functional groupings.
For your normal visitors, their pages/views uses the default layout and for the admin users, they get the views rendered from the admin area where it has it's own layout.
For the views from admin area (or even the _Viewstart.cshtml
), you can explicitly specify the admin layout
@{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
Upvotes: 4