【问题标题】:Updating user data with ApplicationUser class - ASP.NET Identity使用 ApplicationUser 类更新用户数据 - ASP.NET Identity
【发布时间】:2017-05-30 23:45:28
【问题描述】:

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

我尝试为与数据库列对应的每个属性分配一个值,以防它在 if 语句中为空,但这并没有解决问题,也没有错误..
我发现这篇文章中的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>

【问题讨论】:

  • 如果这三个值是您绑定到视图的仅有的三个值,那么您发布的信息不足以让 EF 找到匹配的实体。
  • 如何解决这个问题?邮箱地址肯定是唯一的,EF应该能找到。
  • 仅当该字段被标记为主键字段时。 “解决”这个问题的正确方法是不使用数据模型作为视图模型。
  • manager.Update(editUsersModel);仅适用于 ApplicationUser 类型的对象。制作另一个模型只是为了将 ApplicationUser 对象的每个属性复制到我的模型对象并查询每个模型属性以更改数据库中的值,我发现它的设计很糟糕.. :)
  • “对每个模型属性进行查询以更改数据库中的值”-我不确定您的意思。假设您的视图模型具有您的主键字段(例如,ID),您将执行var user = context.Users.SingleOrDefault(u =&gt; u.ID == model.ID) 之类的操作,然后更新相关字段。无论如何,我假设您只需要为您的主键字段添加一个隐藏的输入字段。

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


【解决方案1】:

[P.S.] 到目前为止我应用的解决方案,它工作正常:

{
        if (editUserModel.UserName == null)
        {
            editUserModel.UserName = editUserModel.Email;
        }
        if (ModelState.IsValid)
        {
            db.Entry(editUserModel).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("UsersList");
        }
        return View(editUserModel);
}

出于某种原因,在我看来,“用户名”是标识用户行所必需的某种键。

【讨论】:

    猜你喜欢
    • 2014-05-15
    • 2019-01-31
    • 2014-02-16
    • 2017-01-25
    • 1970-01-01
    • 2013-12-24
    • 2018-01-21
    • 2018-07-14
    • 1970-01-01
    相关资源
    最近更新 更多