Get UserName by UserId and display in View

只谈情不闲聊 提交于 2019-12-13 22:15:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!