简体   繁体   English

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

[英]Updating user data with ApplicationUser class - ASP.NET Identity

I have an Edit page for users, which works fine for GET, but for POST AplicationUser object is not saved in database. 我有一个针对用户的“编辑”页面,该页面适用于GET,但对于POST而言,AplicationUser对象未保存在数据库中。 I mention that some of the object fields are null, but it's passed to POST method successfully. 我提到有些对象字段为null,但已成功将其传递给POST方法。

I tried to assign a value for each property corresponding to database column in case it's null with if statements, but that haven't solved the problem, no errors either.. 我尝试为与数据库列相对应的每个属性分配一个值,以防if语句为null,但这并没有解决问题,也没有错误。
I figured out that Updating user data - ASP.NET Identity second answer (JoshdeVries) from this post may be related to my case. 我发现这篇文章中的更新用户数据 -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);
    }

And in View 3 input fields like this(I need to fill more fields but these 3 are for test): 在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>

[PS] Solution that I applied so far and it's working fine: [PS]到目前为止,我已经应用了并且运行良好的解决方案:

{
        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);
}

For some reason it seems to me that 'UserName' is some kind of Key that is mandatory to indentify the user row. 出于某种原因,在我看来,“ UserName”是某种密钥,对于标识用户行是必不可少的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET标识中添加与ApplicationUser类的关系(数据库优先) - Add relationships to the ApplicationUser class in ASP.NET Identity (Database First) ASP.NET身份-ApplicationUser和我的自定义模型/实体 - ASP.NET Identity - ApplicationUser and my custom models / entities ASP.NET MVC 5 - 身份。 如何获取当前的ApplicationUser - ASP.NET MVC 5 - Identity. How to get current ApplicationUser ASP.NET Core Identity不会注入UserManager <ApplicationUser> - ASP.NET Core Identity does not inject UserManager<ApplicationUser> 是否可以重写ASP.NET Identity的ApplicationUser? - Is is possible to Override ASP.NET Identity's ApplicationUser? ApplicationUser 始终为空 ASP.NET CORE 3.0 标识 - ApplicationUser always null ASP.NET CORE 3.0 Identity ASP.NET Identity Framework ApplicationUser不是模型的一部分 - ASP.NET Identity Framework ApplicationUser is not part of the model ASP.NET身份-更新用户记录后保持登录 - ASP.NET Identity - Maintain login after updating user record 在 asp.net 内核中更新身份用户时并发失败 - Concurrency failure while updating identity user in asp.net core 通过 ASP.NET Identity 2 中的 UserManager.Update() 更新用户 - Updating user by UserManager.Update() in ASP.NET Identity 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM