2014-12-01 45 views
0

我一直在關注本教程的添加配置文件數據到用戶類部分,以便在MVC 5的註冊頁面中添加更多字段。到目前爲止,它工作正常,我沒有問題。現在的問題是我不知道如何顯示在管理頁面上,用戶可以在其中查看他的個人資料信息,如更改密碼。例如,我想添加該頁面上的名字和姓氏。在MVC5中添加用戶數據以管理頁面

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on#ap

下面是我在談論的頁面截圖: http://puu.sh/dcMmj/82ddd8fc97.PNG

我的項目是什麼Visual Studio中的註冊頁面添加名字和姓氏爲您創建。在下面的身份模式就像在教程

public class ApplicationUser : IdentityUser 
{ 
    public string FirstName{ get; set; } 
    public string LastName { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 

我想我需要在這裏ManageController.cs添加的東西,但我不知道添加了此。

// GET: /Manage/Index 
    public async Task<ActionResult> Index(ManageMessageId? message) 
    { 
     ViewBag.StatusMessage = 
      message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." 
      : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." 
      : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set." 
      : message == ManageMessageId.Error ? "An error has occurred." 
      : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added." 
      : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed." 
      : ""; 

     var model = new IndexViewModel 
     { 
      HasPassword = HasPassword(), 
      PhoneNumber = await UserManager.GetPhoneNumberAsync(User.Identity.GetUserId()), 
      TwoFactor = await UserManager.GetTwoFactorEnabledAsync(User.Identity.GetUserId()), 
      Logins = await UserManager.GetLoginsAsync(User.Identity.GetUserId()), 
      BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(User.Identity.GetUserId()), 

      // I think it goes in here somewhere 


     }; 
     return View(model); 
    } 

回答

2

您可以使用此代碼替換您的VAR模型

ApplicationUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); 

var model = new IndexViewModel { 
    HasPassword = HasPassword(), 
    PhoneNumber = user.PhoneNumber, 
    TwoFactor = user.TwoFactorEnabled, 
    Logins = await UserManager.GetLoginsAsync(user), 
    BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(user), 
    FirstName = user.FirstName   
} 

現在你可以在你的視圖中使用model.FirstName。

+0

我的項目是Visual Studio爲您創建的註冊頁面中添加的名字和姓氏。從教程向我展示的內容中添加了身份模型中的額外內容。 – Shido 2014-12-01 16:39:08

+0

不錯!感謝您花時間幫助我。它現在正在工作,但它給了我一個錯誤,直到我加入等待線路。如果有人遇到類似問題,我會在這裏發佈更改。 'ApplicationUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId());' – Shido 2014-12-01 18:05:49

+0

我試着讓它工作。但是我得到錯誤Models.indexViewModel不包含'name'的定義。 – 2015-03-09 20:46:26

相關問題