How to use multiple layouts in MVC 3?

I have four kinds of users (client, administrator, manager, teacher) who can use my future ASP website ... And so for each of them I need to create different interfaces ...

And so my questions are:

  • Is it correct to use a different layout in MVC 3? If not, what can I use for my problem?

  • If it fixes using a different layout in MVC 3? Can you give me one or more examples, please?

+3
source share
4 answers

You can display pages in different ways by checking which user they are.

In my _Layout.cshtml, I have this:

@if (Request.IsAuthenticated && HttpContext.Current.User.IsInRole("Interviewer")) { <script type="text/javascript"> $("#logindisplay").show(); </script> <li>@Html.ActionLink("Forside", "Index", "Home")</li> <li>@Html.ActionLink("Spørgeskema", "Index", "Survey2")</li> <li>@Html.ActionLink("Brugere", "Index", "UserAdministration")</li> <li>@Html.ActionLink("Statistik", "Index", "Statistik")</li> <li>@Html.ActionLink("Vagtplan", "Vagtplan", "Statistik")</li> } @if (HttpContext.Current.User.IsInRole("Respondent")) { <li>@Html.ActionLink("Gammelt spørgeskema", "Index")</li> } 

Etc.

You can create different DisplayTemplates for each kind of role and display them based on the role that the user has.

To manually assign roles to different users, use the ASP.NET configuration

ASP.NET Configuration

From there, you can create your roles and manage users.

You do not want to do this in the long run if you get a lot of users on your site. Instead, when they create an account, you should automatically assign your role.

You can do this in your AccountController, like so:

 if (createStatus == MembershipCreateStatus.Success) { Roles.AddUserToRole(model.UserName, "Respondent"); } .... 

Your model may have a Role property instead of hardcoding.

+1
source

I would create different _Layout.cshtml pages for each user category and place the _Layout selection logic on the _ViewStart.cshtml page.

[Since _ViewStart.cshtml allows us to write code, we can optionally make our layout selection logic richer than just a basic set of properties.]

http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx

+8
source

I use 2 layouts in my applications - Master (for all users) and Admin (for the admin team). The only difference in the scenery is that the administrator does not have banners, logos, etc. .... Thus, it is up to you to use several. But I would be left with one for the Client, Manager and Teacher. Use different CCS files to make them look unique.

+1
source

I found this answer perfectly. Adding to it, if you want to use a different layout based on the name of the controller, try editing the method as shown below:

  public static string LayoutHelper(RouteData data, string defaultLayout = "") { if (data.Values["controller"].ToString() == "client") return "~/views/shared/_Layout2.cshtml"; return defaultLayout; } 

View client controller index

 @{ Layout = HtmlHelper.LayoutHelper(Request.RequestContext.RouteData, "~/views/shared/_layout1.cshtml"); ViewBag.Title = "Clients";} <h2>This is my view</h2> 

And finally, the new Layout2.cshtml

 <!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /><title>@ViewBag.Title</title></head><body> This is my Layout2 <div> RenderBody()</div></body></html> 
0
source

Source: https://habr.com/ru/post/923702/


All Articles