I have created both the login and registration page in 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.
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)
You can try the following method to return the list of users when the user is 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>
}
Users:
public class Users
{
public string Name { get; set; }
public Role role { get; set; }
}
public enum Role
{
Admin=1,
NonAdmin=2
}
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.