如何使用多个布局MVC 3? [英] How to use multiple Layout in MVC 3?

查看:67
本文介绍了如何使用多个布局MVC 3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经4种用户(客户,管理,经理,教师),可以使用我的未来ASP网站......等等为他们每个人我都创造不同的接口...

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

所以我的问题:


  • 这是正确的在MVC 3使用不同的布局?如果不是我可以用我的问题?

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

如果它是正确的如何在MVC 3使用不同的布局?你能给我一个或多个例子吗?

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

推荐答案

您可以通过测试它是什么样的用户显示不同的方式页

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

在我的_Layout.cshtml我有这样的:

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

等。

您可以为每个样的角色创建不同的DisplayTemplates和显示这些基于哪个角色的用户。

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

要手动角色分配给不同的用户,使用ASP.NET配置

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.

您可以做到这一点在你的AccountController,比如像这样的:

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.

这篇关于如何使用多个布局MVC 3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆