-1

I am currently trying to display one user from the list after the user clicks the log in button, I only want to display the newly registered user and the list of users that are already there should be optional to view

I have this in my controller

public ActionResult ViewUsers()
{
    DataAccessLyer dal = new DataAccessLayer();
    List<UserDetailModel> list = dal.GetUserList();
    return View(list);
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120

1 Answers1

0

You can get the currently logged in user from the Controller.User property.

Use this information the filter the list of all users.

public ActionResult ViewUsers() {
    DataAccessLyer dal = new DataAccessLayer();
    List<UserDetailModel> allUsers = dal.GetUserList();
    var currentUserName = User.Identity.Name;
    var filteredUsers = allUsers.Where(u => u.LoginName == currentUserName).ToList();
    return View(filteredUsers);
}

How to get the current user in ASP.NET MVC

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36