N
N
Nikolai Retsinkevich2015-10-01 12:32:09
ASP.NET
Nikolai Retsinkevich, 2015-10-01 12:32:09

How to get UserName by UserId in ASP MVC 5?

I have a project on ASP MVC 5, and in the database in the table there are user ids who performed a certain action. In the view, I need to display their login. How to get UserName by UserId in Owin? How to do this for example in the controller I know -

private ApplicationUserManager _userManager;
protected ApplicationUserManager UserManager
{
    get { return _userManager ?? (_userManager = 
     HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>()); }
}

var user = UserManager.FindById(id);
string username = user.UserName;

but how to use it in views?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2015-10-01
@dmitryKovalskiy

1) Throw it into the ViewBag
2) Throw it into the Model for the View
3) Add the necessary using and hack HttpContext.Current (I don’t advise doing this)
4) Make the GetUserName method on the Model class - which conjures, shamanizes and at least somehow pulls out the desired parameter .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question