2013-12-08 77 views
1

我正在嘗試改編注冊和管理與新的AspNetIdentity(VS 2013/MVC5/EF6)的框出來的意見。我一直非常成功地擴展了註冊屬性,將新元素與設置的用戶名和密碼註冊相結合,但是現在我無法在填充新字段或管理帳戶視圖中的任何字段時感到痛苦。我試圖添加@ Html.HiddenFor(model => model.Id)到視圖,但即使我添加var id = User.Identity.GetUserId();到AccountController。我一直盯着AccountController比較代碼和另一個腳手架的CRUD「Edit」控件。我是否需要一個新的公共的ActionResult或公共的異步任務,或者我可以適應已經存在的那些?擴展「Manage.cshtml」開箱VS 2013

我抵制張貼代碼,因爲它是從字面上,但任何幫助將不勝感激。

UPDATE。

現在這個節拍全部..我在本週末在家工作,通過遠程訪問將我的文件傳輸到我的工作機器上進行遠程測試,然後瞧!我的用戶名和舊密碼(當然是散列)正確顯示在我的Manage.cshtml中。但現在我正坐在我的工作電腦 - 這些領域再次空白..

我抵制,但這是我的代碼; AccountViewModel.cs

public class ManageUserViewModel 
{ 
    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Current password")] 
    public string OldPassword { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)] 
    [DataType(DataType.Password)] 
    [Display(Name = "New password")] 
    public string NewPassword { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm new password")] 
    [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 
    public string UserName { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    [Required] 
    [DataType(DataType.EmailAddress)] 
    public string Email { get; set; } 
} 

AccountController.cs

// GET: /Account/Manage 
public ActionResult Manage(ManageMessageId? message) 
{ 
    ViewBag.StatusMessage = 
     message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." 
     : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." 
     : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed." 
     : message == ManageMessageId.Error ? "An error has occurred." 
     : ""; 
    ViewBag.HasLocalPassword = HasPassword(); 
    **var user = new ApplicationUser() 
    { 
     UserName = ViewBag.UserName, 
     FirstName = ViewBag.FirstName, 
     LastName = ViewBag.LastName, 
     Email = ViewBag.Email 
    };** 

    ViewBag.ReturnUrl = Url.Action("Manage"); 
    return View(); 
} 

// 
// POST: /Account/Manage 
[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Manage(ManageUserViewModel model) 
{ 
    bool hasPassword = HasPassword(); 
    ViewBag.HasLocalPassword = hasPassword; 
    ViewBag.ReturnUrl = Url.Action("Manage"); 
    if (hasPassword) 
    { 
     if (ModelState.IsValid) 
     { 
      IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword); 
      ViewBag.UserName = model.UserName; 
      ViewBag.FirstName = model.FirstName; 
      ViewBag.LastName = model.LastName; 
      ViewBag.Email = model.Email; 
      if (result.Succeeded) 
      { 
       return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess }); 
      } 
      else 
      { 
       AddErrors(result); 
      } 
     } 
     else 
     { 
      // User does not have a password so remove any validation errors caused by a missing OldPassword field 
      ModelState state = ModelState["OldPassword"]; 
      if (state != null) 
      { 
       state.Errors.Clear(); 
      } 
      if (ModelState.IsValid) 
      { 
       IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword); 
       ViewBag.UserName = model.UserName; 
       ViewBag.FirstName = model.FirstName; 
       ViewBag.LastName = model.LastName; 
       ViewBag.Email = model.Email; 
       if (result.Succeeded) 
       { 
        return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess }); 
       } 
       else 
       { 
        AddErrors(result); 
       } 
      } 
     } 
    } 
    // If we got this far, something failed, redisplay form 
    return View(model); 
} 

對於一兩件事,那新用戶清除我的所有領域 - 但我太newbee來完全得到我需要的語法保存數據...

+0

您是否向該模型添加了屬性? – SLaks

+0

是的,我將它添加到模型中,但我在哪裏以及如何將它添加到控制器中掙扎。 – Alex

回答

0

YESSSSSS !!!!

終於搞清楚我做錯了什麼;

AccountController.cs - var user = new ApplicationUser()是我的失敗。現在用它取而代之;

 ViewBag.HasLocalPassword = HasPassword(); 
     var user = UserManager.FindById(User.Identity.GetUserId()); 
     var model = new ManageUserViewModel 
     { 
      UserName = user.UserName, 
      FirstName = user.FirstName, 
      LastName = user.LastName, 
      Email = user.Email, 
      OldPassword = user.PasswordHash 
     }; 
     return View(model); 
    } 

現在我的字段已填充 - 但誰能告訴我爲什麼OldPassword字段沒有填充?

接下來,我會測試它是否實際上更新所有字段!