问题描述
我在 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;
}
}
结果: