扩展“Manage.cshtml”开箱即用VS 2013

时间:2013-12-08 17:23:36

标签: asp.net-mvc visual-studio-2013 asp.net-mvc-5

我正在尝试使用新的AspNetIdentity(VS 2013 / MVC5 / EF6)调整注册和管理开箱即用的视图。我已经非常成功地扩展了注册属性,将新元素与设置的用户名和密码注册相结合,但现在我在填充新字段或管理帐户视图中的任何字段时都很失败。我尝试将@ Html.HiddenFor(model => model.Id)添加到视图中,但即使我添加了var id = User.Identity.GetUserId();到AccountController。我一直在盯着AccountController将代码与另一个脚手架的CRUD“编辑”控件进行比较。我是否需要一个新的公共ActionResult或公共异步任务,还是我可以调整已经存在的那些?

我拒绝发布代码,因为它实际上是OUT OF THE BOX,但任何帮助将不胜感激。

更新。

现在这打败了所有..本周末我在家工作,通过远程访问将我的文件传输到我的工作机器远程测试,瞧!在我的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);
}

首先,这个新用户正在清除我的所有字段 - 但是我太过新手了,无法获得保存数据所需的语法......

1 个答案:

答案 0 :(得分: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字段没有填充吗?

接下来我将测试它是否实际更新了所有字段!