编辑 ASP.NET Core Identity 中的用户
Editing users in ASP.NET Core Identity
我只是想为我的身份数据库创建一个编辑页面。我正在使用 Razor Pages,而且我是新手。我找到了一些 MVC 的解决方案,但由于使用了视图和控制器等其他东西,它们并不真正适合我。我有一个主页 Index.cshtml,我可以在其中从如下图所示的列表中选择用户
然后单击提交按钮将其删除。
我是这样删除的:
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>
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");
}
它对我来说工作得很好,但我也需要编辑。所以我在 Index.cshtml.cs 中的 Edit POST 方法看起来像:
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");
}
我的 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");
}
这当然行不通。我只是想为 Razor Pages 重新制作一些 MVC 示例。也许你有更好的解决方案,但我真的卡在这里了。
所以,是的,我刚刚在我的 Index.cshtml 页面上做了一个这样的按钮,它提供了 asp-route-id 来编辑页面:
<a asp-page="Edit" class="btn btn-sm btn-primary" asp-route-id="@user.Id">Edit</a>
在 Edit.cshtml.cs 文件上创建了一个 InputModel:
public InputModel Input { get; set; }
public class InputModel
{
[Required(ErrorMessage ="{0} не может быть пустым")]
[EmailAddress(ErrorMessage ="Неверный формат {0}")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "Введите {0}")]
[StringLength(5, ErrorMessage = "{0} должна быть из {1} символов", MinimumLength = 5)]
[Display(Name = "Последовательность")]
public string Sequence { get; set; }
...
}
刚刚添加到 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");
}
我只是在 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>
我只是想为我的身份数据库创建一个编辑页面。我正在使用 Razor Pages,而且我是新手。我找到了一些 MVC 的解决方案,但由于使用了视图和控制器等其他东西,它们并不真正适合我。我有一个主页 Index.cshtml,我可以在其中从如下图所示的列表中选择用户
然后单击提交按钮将其删除。
我是这样删除的:
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>
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");
}
它对我来说工作得很好,但我也需要编辑。所以我在 Index.cshtml.cs 中的 Edit POST 方法看起来像:
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");
}
我的 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");
}
这当然行不通。我只是想为 Razor Pages 重新制作一些 MVC 示例。也许你有更好的解决方案,但我真的卡在这里了。
所以,是的,我刚刚在我的 Index.cshtml 页面上做了一个这样的按钮,它提供了 asp-route-id 来编辑页面:
<a asp-page="Edit" class="btn btn-sm btn-primary" asp-route-id="@user.Id">Edit</a>
在 Edit.cshtml.cs 文件上创建了一个 InputModel:
public InputModel Input { get; set; }
public class InputModel
{
[Required(ErrorMessage ="{0} не может быть пустым")]
[EmailAddress(ErrorMessage ="Неверный формат {0}")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "Введите {0}")]
[StringLength(5, ErrorMessage = "{0} должна быть из {1} символов", MinimumLength = 5)]
[Display(Name = "Последовательность")]
public string Sequence { get; set; }
...
}
刚刚添加到 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");
}
我只是在 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>