C# 使用ApplicationUser类-ASP.NET标识更新用户数据
我有一个针对用户的编辑页面,对于GET来说效果很好,但对于POST应用程序来说,用户对象并没有保存在数据库中。我提到一些对象字段为null,但它已成功地传递给POST方法 我尝试为数据库列对应的每个属性分配一个值,以防if语句为null,但这并没有解决问题,也没有错误。C# 使用ApplicationUser类-ASP.NET标识更新用户数据,c#,asp.net,sql-server,asp.net-mvc,asp.net-identity,C#,Asp.net,Sql Server,Asp.net Mvc,Asp.net Identity,我有一个针对用户的编辑页面,对于GET来说效果很好,但对于POST应用程序来说,用户对象并没有保存在数据库中。我提到一些对象字段为null,但它已成功地传递给POST方法 我尝试为数据库列对应的每个属性分配一个值,以防if语句为null,但这并没有解决问题,也没有错误。 我发现这篇文章的第二个答案(JoshdeVries)可能与我的案件有关 [HttpPost] [ValidateAntiForgeryToken] 公共操作结果编辑([Bind(Include=“Id,Name,Email,P
我发现这篇文章的第二个答案(JoshdeVries)可能与我的案件有关
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果编辑([Bind(Include=“Id,Name,Email,Password,ConfirmPassword,Roles,PhoneNumber”)]ApplicationUser editUserModel)
{
if(ModelState.IsValid)
{
var store=new UserStore(new ApplicationDbContext());
var-manager=新用户管理器(存储);
var result manager.Update(editUserModel);
如果(!result.successed)//这是false,则更新失败!
{
Console.WriteLine(result.Errors.ToList());
返回视图(editUserModel);
}
store.Context.SaveChanges();
返回重定向到操作(“用户列表”);
}
返回视图(editUserModel);
}
在视图中,有3个这样的输入字段(我需要填写更多字段,但这3个字段用于测试):
@LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”})
@LabelFor(model=>model.Email,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Email,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Email,“,new{@class=“text danger”})
@LabelFor(model=>model.PhoneNumber,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PhoneNumber,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.PhoneNumber,“,new{@class=“text danger”})
[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);
}
出于某种原因,“UserName”似乎是标识用户行所必需的某种键。如果这是您绑定到视图的仅有三个值,则您没有发布足够的信息,EF无法找到匹配的实体。如何解决此问题?电子邮件地址当然是唯一的,EF应该能够找到它。只有当该字段被标记为主键字段时。“修复”此问题的正确方法是不将数据模型用作视图模型.manager.Update(editUsersModel);将仅适用于ApplicationUser类型的对象。创建另一个模型只是为了将ApplicationUser对象的每个属性复制到我的模型对象,并查询每个模型属性以更改数据库中的值,我发现这是一个糟糕的设计:)“对每个模型属性进行查询以更改数据库中的值”-我不确定这是什么意思。假设视图模型有主键字段(比如,
ID
),您可以执行类似于var user=context.Users.SingleOrDefault(u=>u.ID==model.ID)
,然后更新相关字段。无论如何,我假设您只需要为主键字段添加一个隐藏的输入字段。
<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>
{
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);
}