2016-04-22 8 views
3

ビュー/管理/ Index.cshtmlを変更して、ユーザーの電子メールも表示しました。私はIndexViewModelも変更して "Email"文字列を認識し、デフォルトでそこにある電話番号1の変更と同様の別の.cshtmlページを作成しました。新しいページが、私が見たものからChangeEmail.cshtmlASP.NET ID編集がユーザーのデータに記録されていますか?

@using (Html.BeginForm("ChangeEmail", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{ 
    @Html.AntiForgeryToken() 
    <h4>Add an email</h4> 
    <hr /> 
    @Html.ValidationSummary("", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.Email, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(model => model.Email, new { @class = "form-control" }) 

     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" class="btn btn-default" value="Submit" /> 
     </div> 
    </div> 
} 

と呼ばれ、パスワードの変更がUserManager.cs 内部「ChangePasswordAsync」と呼ばれるタスクを介して行わせることなく、メールアドレスを変更する方法はあります新しい仕事?

EDIT:コントローラからより多くを追加(インデックス):

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." 
        : message == ManageMessageId.EmailChangedSuccess ? "Your email has been changed" 
        : ""; 

       var userId = User.Identity.GetUserId(); 
       var userEmail = User.Identity.Name; 
       var user = UserManager.FindById(userId); 



       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 = user.Email, 
        City = user.City, 
        Region = user.Region 


       }; 
       user.Email = "[email protected]"; 
       UserManager.UpdateAsync(user); 

       return View(model); 
      } 

[HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> ChangeEmail(ChangeEmailViewModel model) 
     { 
      if (!ModelState.IsValid) 
      { 
       return View(model); 
      } 



      return RedirectToAction("Index", new { Message = ManageMessageId.EmailChangedSuccess }); 
     } 
+0

'userManager.UpdateAsync(user);' – jamiedanq

答えて

3

があなたのChangeEmailViewModelからユーザーの電子メールアドレスを取得し、その後userManager.UpdateAsync(user)

EDIT

[HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> ChangeEmail(ChangeEmailViewModel model) 
     { 
      if (!ModelState.IsValid) 
      { 
       return View(model); 
      } 
      //Get the user's Id 
      var userId = User.Identity.GetUserId(); 

      //get the user (you can modify the variables to fit yours) 
      var user = UserManager.FindByIdAsync(userId); 

      //this is how to change the Email 
      user.Result.Email= model.Email// **EDIT**; 

     userManager.UpdateAync(user.Result); 

      return RedirectToAction("Index", new { Message = ManageMessageId.EmailChangedSuccess }); 
     } 
を使用して、ユーザーの詳細情報を更新
+0

詳細を教えてください。私はどこに行くのか分からないのですか?ありがとう – crystyxn

+0

あなたの投稿を使用して、回答に詳細を追加しました – jamiedanq

+0

あなたの方法を試しているときに、「現在のコンテキストにuserIdが存在しません」というメッセージが表示されます。しかし、私はIndex()メソッド(上記の元のポストを更新しました)を変更することでそれを動作させることができますし、電子メールを "[email protected]"に変更することはできますが、どのようにしてChangeEmail内で行うことができますか?私が逃しているものがあります – crystyxn