问题
In an ASP.NET MVC 5 application how would I go about getting a Username
from a given UserId
and display it in a View
?
Note - I do not need the Username
of the current User
.
回答1:
Based on your comment, you are using ASP.Net Identity. I assume you already have ApplicationUserManager. For example,
private ApplicationUserManager _userManager;
protected ApplicationUserManager UserManager
{
get { return _userManager ?? (_userManager =
HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>()); }
}
Then you can call FindById method.
var user = UserManager.FindById(id);
string username = user.UserName;
来源:https://stackoverflow.com/questions/32828744/get-username-by-userid-and-display-in-view