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 ?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With