微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我在 ASP.NET MVC 5 中创建了登录注册页面注册页面上有一个角色选项,询问用户管理员还是管理员

现在,如果尝试登录用户管理员,那么我必须显示数据库中存在的所有用户,但如果用户 >非管理员然后我必须在页面显示“嗨!用户名

我在下面附上我的 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");
        }
    }
}

解决方法

如果 user==admin 然后 foreach var user in userlist cw(username) 做一个 if 语句 else cw(用户名)

,

当用户为admin时,您可以尝试以下方法返回用户列表。

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>

}

用户:

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

        public Role role { get; set; }
    }



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

控制器:

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;

        }

    }

结果:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。