Buda Cristian
Buda Cristian

Reputation: 277

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)

Upvotes: 1

Views: 838

Answers (1)

Paul Fleming
Paul Fleming

Reputation: 24526

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);

Upvotes: 4

Related Questions