How to use multiple Layout in MVC 3?

2020-06-27 02:07发布

问题:

I've four kinds of user (Customer, Admin, Manager, teacher) that can use my future ASP Web Site... And so for each of them I have to create different interfaces...

And so my questions:

  • Is this correct to use different Layout in MVC 3 ? If not what can I use for my problem ?

  • If it's correct how to use different layout in MVC 3 ? Can you give me one or more example please ?

回答1:

You can show the pages in different ways by testing what kind of user it is

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>
                    }

And so on.

You could create different DisplayTemplates for each kind of role and display these based upon which role the user has.

To manually assign Roles to different users, use 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 would want to assign their role automatically.

You can do this in your AccountController, for instance like this:

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

Your model could have the Role property instead of hardcoding it.



回答2:

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

[Because the _ViewStart.cshtml allows us to write code, we can optionally make our Layout selection logic richer than just a basic property set.]

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



回答3:

I use 2 Layouts in my applications - Master (for all users) and Admin (for admin team). The only difference is in decorations - Admin has no banners, logos, etc... So, it is up to you to use several. But I'd stay with one for Customer, Manager and Teacher. Use different CCS files for them to make appearance unique



回答4:

I found this answer great. Adding to it, if you want use different layout based on the controller name then try editing method like below:

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

Client Controller index view

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

And at last 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>