根据用户角色的类型更改布局 [英] change Layout based on type of User role

查看:62
本文介绍了根据用户角色的类型更改布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC的新手,我浏览了堆栈中的类似文章,但它们似乎并没有解决我的问题.我有两个角色Admin和User.当一个人登录时,我希望根据该人是否具有 Admin 特权或 User 特权来更改布局.

I am new to MVC, I looked through similar posts on stack, but they did'nt seem to help my problem. I have two roles Admin and User. When a person logs in, I want the layout to change based on whether the person had Admin privileges or User Privileges.

我的 Admin 布局为_Layout.cshtml

我的 User 布局是_LayoutUser.cshtml

我的登录控制器中是否可以放置某些内容,这些内容将根据用户的角色来更改布局?用户的角色在登录"表中指定.

Is there something in my Login Controller that I can put which will change the layout based on the role of the User? The role of the user is specified in the Login table.

id     LoginStudentNumber    LoginPassword      Role
1           123456              123456          Admin
2           234567              234567          Admin
3           345678              345678          User
4           456789              456789          User

登录控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.DAL;
using RMS.Models;

namespace RMS.Controllers
{
    public class LoginController : Controller
    {
        RMSContext db = new RMSContext();


        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(Login model, int? LoginStudentNumber, string LoginPassword)
        {


            //RMS.Models.Login user = db.Logins.Find(LoginID);
            RMS.Models.Login user = db.Logins.FirstOrDefault(l => l.LoginStudentNumber == model.LoginStudentNumber);


            if (user == null)
            {
               ViewBag.InvalidID = "Invalid User Id";

                return View(model);
            }



            if ((user.LoginPassword.Equals(LoginPassword)) && (user.Role.Equals("Admin")))

            {

                return RedirectToAction("Index", "Home");


            }

            if ((user.LoginPassword.Equals(LoginPassword)) && (user.Role.Equals("User")))
            {

                return RedirectToAction("IndexUser", "Home");


            }


            ViewBag.InvalidPassword = "Invalid Password";
            return View(model);

        }
    }
}

_Layout.cshtml页面:

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Wits RMS</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body onload=display_ct();>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a href="@Url.Action("Index", "Home")">
                    <img src="~/Icons/Wits---Logo---Mono-Neg---Colloquial---Ext.png" style="height:45px; width:80px; margin-right:5px;" alt="" />
                </a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Researcher", "Index", "Researcher")</li>
                    <li>@Html.ActionLink("Submission", "Index", "Submission")</li>
                    <li>@Html.ActionLink("Publication", "Index", "Publication")</li>
                    <li>@Html.ActionLink("Faculty", "Index", "Faculty")</li>
                    <li>@Html.ActionLink("School", "Index", "School")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Reports", "Index", "Reports")</li>
                    <li>@Html.ActionLink("Logout", "Login", "Login")</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>            
            <script type="text/javascript">
                function display_c() {
                    var refresh = 1000; // Refresh rate in milli seconds
                    mytime = setTimeout('display_ct()', refresh)
                }

                function display_ct() {
                    var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
                    ];
                    var dayOfWeek = ["Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
                    var strcount
                    var x = new Date()
                    var x1 = dayOfWeek [x.getDay()] + ", " + x.getDate() + " " + monthNames[x.getMonth()] + " " + x.getFullYear();
                    x1 = x1 + " " + ('0' + x.getHours()).slice(-2) + ":" + ('0' + x.getMinutes()).slice(-2) + ":" + ('0' + x.getSeconds()).slice(-2);
                    document.getElementById('ct').innerHTML = x1
                    tt = display_c();
                }
            </script>

            <p>&copy; @DateTime.Now.Year - Wits RMS <span style="float:right; top:20px;" id='ct'></span></p>                             
        </footer>
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

_LayoutUser.cshtml页面:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Wits RMS</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body onload=display_ct();>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a href="@Url.Action("IndexUser", "Home")">
                    <img src="~/Icons/Wits---Logo---Mono-Neg---Colloquial---Ext.png" style="height:45px; width:80px; margin-right:5px;" alt="" />
                </a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">                    
                    <li>@Html.ActionLink("Submission", "IndexUser", "Submission")</li>
                    <li>@Html.ActionLink("Publication", "Index", "Publication")</li>                    
                    <li>@Html.ActionLink("Contact", "ContactUser", "Home")</li>
                    <li>@Html.ActionLink("About", "AboutUser", "Home")</li>
                    <li>@Html.ActionLink("Logout", "Login", "Login")</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <script type="text/javascript">
                function display_c() {
                    var refresh = 1000; // Refresh rate in milli seconds
                    mytime = setTimeout('display_ct()', refresh)
                }

                function display_ct() {
                    var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
                    ];
                    var dayOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
                    var strcount
                    var x = new Date()
                    var x1 = dayOfWeek[x.getDay()] + ", " + x.getDate() + " " + monthNames[x.getMonth()] + " " + x.getFullYear();
                    x1 = x1 + " " + ('0' + x.getHours()).slice(-2) + ":" + ('0' + x.getMinutes()).slice(-2) + ":" + ('0' + x.getSeconds()).slice(-2);
                    document.getElementById('ct').innerHTML = x1
                    tt = display_c();
                }
            </script>

            <p>&copy; @DateTime.Now.Year - Wits RMS <span style="float:right; top:20px;" id='ct'></span></p>
        </footer>
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

推荐答案

您只需要修改/Views/_ViewStart.cshtml文件.

@{
    if (this.User.IsInRole("Admin") || !this.User.Identity.IsAuthenticated) {
        Layout = "~/Views/Shared/_Layout.cshtml";
    } else {
        Layout = "~/Views/Shared/_LayoutUser.cshtml";
    }
}

您没有提到如果没有用户登录该怎么办,所以我添加了支票并将其发送到_Layout.cshtml视图.您应该根据应用程序的需要更改逻辑.

You didn't mention what should happen if no user is logged in, so I added the check and just sent them to the _Layout.cshtml view. You should change the logic as needed for your application.

这篇关于根据用户角色的类型更改布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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