简体   繁体   中英

ASP.NET Identity retrieving and editing user data inside the Manage View?

So I've got an ASP.NET MVC 5 application. When you login you can click on the link that says "Hello, !" and you can see the Manage View, which is a default page made by the template, which uses the IndexViewModel.

I succesfully made it show the user's email by adding

public string Email { get; set; }

in IndexViewModel and then in the ManageController

var model = new IndexViewModel          
{
    HasPassword = HasPassword(),
    PhoneNumber = await UserManager.GetPhoneNumberAsync(userId),
    TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId),
    Logins = await UserManager.GetLoginsAsync(userId),
    BrowserRemembered = await  AuthenticationManager.TwoFactorBrowserRememberedAsync(userId),
    Email = await UserManager.GetEmailAsync(userId)           
};

However this was easy, as the UserManager already has a Task called GetEmailAsync. How can I retrieve data from the database like this but for the rest of the fields I've added? (Users are being created by the RegisterStudentViewModel) I've tried to create another Task but it didn't work. How would I go about solving this problem? I want them to be able to see their details and even edit them (such like changing of the Phone Number that already exists)

You can load the entire user entity as follows:

var user = UserManager.Users.FirstOrDefault(u => u.Id == userId);

From there, you can do what you please with the user.

Alternatively, use FindById(userId) :

var user = UserManager.FindById(userId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM