使用ApplicationUser类更新用户数据 - ASP.NET标识

时间:2017-05-30 23:45:29

标签: c# asp.net sql-server asp.net-mvc asp.net-identity

我有一个用户的编辑页面,适用于GET,但是对于POST,AplicationUser对象不会保存在数据库中。我提到一些对象字段为null,但它已成功传递给POST方法。

我尝试为与数据库列对应的每个属性分配一个值,如果它使用if语句为null,但是没有解决问题,也没有错误。
我发现这篇文章的Updating user data - ASP.NET Identity第二个答案(JoshdeVries)可能与我的案例有关。

A photo with the Object in Debugging mode

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Name,Email,Password,ConfirmPassword,Roles,PhoneNumber")] ApplicationUser editUserModel)
    {
        if (ModelState.IsValid)
        {

            var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
            var manager = new UserManager<ApplicationUser>(store);
            var result manager.Update(editUserModel);
            if (!result.Succeeded) // This is false, so Update Failed!
            {
                Console.WriteLine(result.Errors.ToList());
                return View(editUserModel);
            } 
            store.Context.SaveChanges();
            return RedirectToAction("UsersList");
        }
        return View(editUserModel);
    }

在View 3这样的输入字段中(我需要填充更多字段,但这3个字段用于测试):

<div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
</div>

<div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>

<div class="form-group">
        @Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

[P.S。]我到目前为止应用的解决方案,它运行良好:

-z

出于某种原因,在我看来,&#39; UserName&#39;是某种必须识别用户行的密钥。