【问题标题】:Extending "Manage.cshtml" out of the box VS 2013开箱即用 VS 2013 扩展“Manage.cshtml”
【发布时间】:2013-12-25 17:23:46
【问题描述】:

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

我拒绝发布代码,因为它实际上是开箱即用的,但我们将不胜感激。

更新。

现在这比一切都好.. 这个周末我在家工作,通过远程访问将我的文件传输到我的工作机器上,远程测试,瞧!我的 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);
}

一方面,那个新用户正在清除我所有的字段 - 但我太新手了,无法完全掌握保存数据所需的语法......

【问题讨论】:

  • 您是否在模型中添加了属性?
  • 是的,我将它添加到模型中,但我正在努力将其添加到控制器的位置和方式。

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


【解决方案1】:

是的!!!!

终于弄清楚我做错了什么;

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 字段?

接下来我会测试它是否真的在更新所有字段!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多