Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple Layout in MVC 3?

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 ?

like image 920
Razor Avatar asked Dec 16 '22 03:12

Razor


2 Answers

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

like image 119
Min Min Avatar answered Dec 18 '22 16:12

Min Min


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

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.

like image 28
Kenci Avatar answered Dec 18 '22 18:12

Kenci