简体   繁体   English

ASP。 NET MVC 5:如果用户是管理员,则返回所有用户的列表

[英]ASP. NET MVC 5: If user is admin, return the list of all the users

I have created both the login and registration page in ASP.NET MVC 5 .我在ASP.NET MVC 5中创建了登录和注册页面。 There is a role option on the registration page that asks whether the user is an admin or a non-admin .注册页面上有一个角色选项,询问用户是管理员还是非管理员

Now if the user that is trying to log in is an admin then I have to display all the users present in the database but if the user is a non-admin then I have to display "Hi! Name of the User" on the page.现在,如果尝试登录的用户是管理员,那么我必须显示数据库中存在的所有用户,但如果用户是非管理员,那么我必须在页面上显示“嗨!用户名” .

I'm attaching the code of my AccountController below.我在下面附上了我的AccountController的代码。

using MyFirstApp.DBModel;
using MyFirstApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyFirstApp.Controllers
{
    public class AccountController : Controller
    {
        DEV_Training_DemoEntities objDEV_Training_DemoEntities = new DEV_Training_DemoEntities();
        // GET: Account 
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Register()
        {
            UserModel objUserModel = new UserModel();
            return View(objUserModel);
        }

        [HttpPost]
        public ActionResult Register(UserModel objUserModel)
        {
            if(ModelState.IsValid)
            {
                if (!objDEV_Training_DemoEntities.AN_Users.Any(m => m.Username == objUserModel.Username))
                { 
                    AN_Users objUser = new AN_Users();
                    objUser.Name = objUserModel.Name;
                    objUser.DOB = objUserModel.DOB;
                    objUser.Username = objUserModel.Username;
                    objUser.Password = objUserModel.Password;
                    objUser.Gender = objUserModel.Gender;
                    objUser.Role = objUserModel.Role;
                    objDEV_Training_DemoEntities.AN_Users.Add(objUser);
                    objDEV_Training_DemoEntities.SaveChanges();
                    objUserModel.SucessMessage = "You are successfully registered!";
                    return RedirectToAction("Index", "Home");
                }

                else
                {
                    ModelState.AddModelError("Error", "Username already exists!");
                    return View();
                }
            }

            return View();
        }

        public ActionResult Login()
        {
            LoginModel objLoginModel = new LoginModel();
            return View(objLoginModel);
        }

        [HttpPost]
        public ActionResult Login(LoginModel objLoginModel)
        {
            if(ModelState.IsValid)
            {
                if (objDEV_Training_DemoEntities.AN_Users.Where(m => m.Username == objLoginModel.Username && m.Password == objLoginModel.Password).FirstOrDefault() == null)
                {
                    ModelState.AddModelError("Error", "Invalid Email-ID or Password!");
                    return View();
                }
                else
                {
                    var curruser = objDEV_Training_DemoEntities.AN_Users.Where(m => m.Username == objLoginModel.Username && m.Password == objLoginModel.Password).FirstOrDefault();
                    if(curruser.Role)
                    Session["Username"] = objLoginModel.Username;
                    return RedirectToAction("Index", "Home");
                }
            }
            return View();
        }

        public ActionResult Logout()
        {
            Session.Abandon();
            return RedirectToAction("Index", "Home");
        }
    }
}

Make an if statement if user==admin then foreach var user in userlist cw(username) else cw(username)做一个 if 语句 if user==admin then foreach var user in userlist cw(username) else cw(username)

You can try the following method to return the list of users when the user is admin.当用户为管理员时,您可以尝试以下方法返回用户列表。

cshtml: .cshtml:

@using WebApplication2.Models
@model Users
@{
    ViewBag.Title = "Index";

}

<h2>Index</h2>
@using (Html.BeginForm("Index", "Account", FormMethod.Post))
{
    <span>Enter Your Name:</span> @Html.TextBoxFor(m => m.Name)
    <br />
    <span>Enter Your Age: </span> @Html.DropDownListFor(m => m.role,
new SelectList(Enum.GetValues(typeof(Role))),
"Select Role")
    <br />

    <input id="Submit" type="submit" value="submit" />

    <hr />

    <strong>UserName: </strong> @Html.DisplayFor(m => m.Name)
    <br />
    <strong>Role: </strong> @Html.DisplayFor(m => m.role)
    <br />

    <b style="color:red">Userlist: @ViewBag.Message</b>

}

Users:用户:

 public class Users
    {
        public string Name { get; set; }

        public Role role { get; set; }
    }



    public enum Role
    { 
        Admin=1,
        NonAdmin=2
    
    }

Controller: Controller:

public class AccountController : Controller
    {


        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Users um)
        {
            if(um.role==Role.Admin)
            {
                var list = GetUserlist();
                foreach (var item in list)
                {
                    ViewBag.Message += item.Name + " | ";
                }
            }
            else
            {
                string str = string.Format("Hi! {0} of the User",um.Name);
                Response.Write("<script language=javascript>alert('" + str + "');</script>");


            }
            return View(um);
        }


        public List<Users> GetUserlist()
        {
            List<Users> users = new List<Users>();
           // here is you get data from database
            return users;

        }

    }

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM