C# 在ASP.NET核心标识中编辑用户
我只是想为我的身份数据库创建一个编辑页面。我用的是剃须刀页面,我是新手。我找到了一些MVC的解决方案,但它们并不适合我,因为它们使用视图和其他类似控制器的东西。我有一个主页Index.cshtml,在这里我可以从下面的图片这样的列表中选择用户 然后单击“提交”按钮将其删除 我是这样处理删除的:C# 在ASP.NET核心标识中编辑用户,c#,asp.net-core,asp.net-core-mvc,asp.net-core-identity,razor-pages,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Core Identity,Razor Pages,我只是想为我的身份数据库创建一个编辑页面。我用的是剃须刀页面,我是新手。我找到了一些MVC的解决方案,但它们并不适合我,因为它们使用视图和其他类似控制器的东西。我有一个主页Index.cshtml,在这里我可以从下面的图片这样的列表中选择用户 然后单击“提交”按钮将其删除 我是这样处理删除的: <form asp-action="Edit" asp-controller="Users"> <div asp-validation-summary="All" class="tex
<form asp-action="Edit" asp-controller="Users">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<input type="hidden" asp-for="Id" />
</div>
<div class="form-group">
<label asp-for="Email" class="control-label">Email</label>
<input type="text" asp-for="Email" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Score" class="control-label">Score</label>
<input type="number" asp-for="Score" class="form-control" />
</div>
...
<div class="form-group">
<input type="submit" asp-page-handler="Edit" value="Save" class="btn btn-default" />
</div>
</form>
CSHTML文件:
<button type="submit" class="btn btn-sm btn-danger" asp-route-id="@user.Id" asp-page-handler="Del">Del</button>
<button type="submit" class="btn btn-sm btn-primary" asp-route-id="@user.Id" asp-page-handler="Change">Change</button>
Del
改变
CSHTML.CS文件:
public ApplicationUser ApUser { get; set; }
public async Task<ActionResult> OnPostDel(string id)
{
ApUsers = _userManager.Users.ToList();
ApUser = await _userManager.FindByIdAsync(id);
if (ApUser != null)
{
IdentityResult result = await _userManager.DeleteAsync(ApUser);
}
//return RedirectToAction("UserChange/Index");
return RedirectToPage("Index");
}
public ApplicationUser ApUser{get;set;}
公共异步任务OnPostDel(字符串id)
{
ApUsers=_userManager.Users.ToList();
ApUser=await\u userManager.FindByIdAsync(id);
if(ApUser!=null)
{
IdentityResult result=await_userManager.deleteAncy(ApUser);
}
//返回重定向到操作(“用户更改/索引”);
返回至Topage(“索引”);
}
我觉得很好,但我也需要编辑。因此,Index.cshtml.cs中的编辑帖子方法如下所示:
public async Task<ActionResult> OnPostChange(string id)
{
ApUsers = _userManager.Users.ToList();
ApUser = await _userManager.FindByIdAsync(id);
if (ApUser == null)
{
return NotFound();
}
else return RedirectToPage("Edit");
}
公共异步任务OnPostChange(字符串id)
{
ApUsers=_userManager.Users.ToList();
ApUser=await\u userManager.FindByIdAsync(id);
if(ApUser==null)
{
返回NotFound();
}
否则返回Topage(“编辑”);
}
我的Edit.cshtml.cs如下所示:
<form asp-action="Edit" asp-controller="Users">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<input type="hidden" asp-for="Id" />
</div>
<div class="form-group">
<label asp-for="Email" class="control-label">Email</label>
<input type="text" asp-for="Email" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Score" class="control-label">Score</label>
<input type="number" asp-for="Score" class="form-control" />
</div>
...
<div class="form-group">
<input type="submit" asp-page-handler="Edit" value="Save" class="btn btn-default" />
</div>
</form>
电子邮件
分数
...
和Edit.cshtml.cs:
public async Task<IActionResult> OnPostEdit(string id)
{
if (ModelState.IsValid)
{
ApUser = await _userManager.FindByIdAsync(id);
if (ApUser != null)
{
ApUser.Email = Email;
ApUser.UserName = Email;
ApUser.Score = Score;
ApUser.Position = Position;
ApUser.Sequence = Sequence;
var result = await _userManager.UpdateAsync(ApUser);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
else
{
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
}
}
return RedirectToAction("Index");
}
PostEdit上的公共异步任务(字符串id)
{
if(ModelState.IsValid)
{
ApUser=await\u userManager.FindByIdAsync(id);
if(ApUser!=null)
{
ApUser.Email=电子邮件;
ApUser.UserName=电子邮件;
ApUser.Score=分数;
ApUser.Position=位置;
ApUser.Sequence=序列;
var result=await\u userManager.UpdateAsync(ApUser);
if(result.successed)
{
返回操作(“索引”);
}
其他的
{
foreach(result.Errors中的变量错误)
{
AddModelError(string.Empty,error.Description);
}
}
}
}
返回操作(“索引”);
}
当然,这是行不通的。我只是想把一些MVC示例重新制作成Razor页面。也许你们有更好的解决方案,但我真的停留在这里。所以,是的,我在Index.cshtml页面上做了一个按钮,它提供了asp路由id来编辑页面:
<a asp-page="Edit" class="btn btn-sm btn-primary" asp-route-id="@user.Id">Edit</a>
刚刚在Edit.cshtml文件@page“{id}”中添加,以通过上一页提供它
OnPost方法提供来自模型的用户更改:
public async Task<IActionResult> OnPostAsync(string id)
{
ApUser = await _userManager.FindByIdAsync(id);
if (!ModelState.IsValid)
{
return Page();
}
ApUser.Email = Input.Email;
ApUser.Score = Input.Score;
ApUser.Sequence = Input.Sequence;
ApUser.Position = 0;
await _userManager.UpdateAsync(ApUser);
Message = ApUser.Email;
return RedirectToPage("Index");
}
公共异步任务OnPostAsync(字符串id)
{
ApUser=await\u userManager.FindByIdAsync(id);
如果(!ModelState.IsValid)
{
返回页();
}
ApUser.Email=Input.Email;
ApUser.Score=输入.Score;
ApUser.Sequence=Input.Sequence;
ApUser.Position=0;
wait_userManager.UpdateAsync(ApUser);
Message=ApUser.Email;
返回至Topage(“索引”);
}
我只是在Edit.cshtml中使用了一个模型
<form method="post" asp-route-id="@Model.ApUser.Id">
<div class="form-group">
<label asp-for="Input.Email"></label>
<input asp-for="Input.Email" class="form-control" value="@Model.ApUser.Email.ToString()" />
<span asp-validation-for="Input.Email" class="text-danger"></span>
</div>
...
<button type="submit" class="btn btn-default">Save</button>
</form>
...
拯救
当然不起作用了这意味着什么?错误消息?浏览器调试控制台所说的“它当然不工作”不是一个很好的问题描述。我看不出这里有一个明确的问题。代码有什么问题?它在做什么与你期望的不同?是否有错误消息?哪些错误消息?很抱歉,真正的问题是“如何使其工作?”,因为我不知道如何将ID参数从Index.cshtml.cs OnPostChange方法(该方法应打开Edit.cshtml页面)发送到Edit.cshtml.cs方法,如OnPostEditTry将asp page=“Edit”
添加到编辑按钮?有关详细信息,请参阅。您需要编辑
页面的获取路径。您正在重定向到那里,因此浏览器将通过GET请求点击它。在那里,您需要显示一个表单,然后通过POST提交,在那里更新值。