通过UserId获取UserName并显示在视图中 [英] Get UserName by UserId and display in View
本文介绍了通过UserId获取UserName并显示在视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在ASP.NET MVC 5应用程序中,如何从给定的UserId
获取Username
并将其显示在View
中?
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
?
注意-我不需要 current User
的Username
.
推荐答案
根据您的评论,您正在使用ASP.Net Identity.我假设您已经有ApplicationUserManager.例如
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>()); }
}
然后您可以调用 FindById 方法.
var user = UserManager.FindById(id);
string username = user.UserName;
这篇关于通过UserId获取UserName并显示在视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文