在ASP.NET Core Identity中编辑用户

时间:2018-05-15 20:28:40

标签: c# asp.net-core asp.net-core-mvc asp.net-core-identity razor-pages

我只是想为我的Identity数据库创建一个Edit页面。我正在使用Razor Pages,而且我是新手。我找到了一些MVC的解决方案,但由于使用了Views和Controllers等其他东西,它们对我来说并不适用。我有一个主页Index.cshtml,我可以从列表中选择用户,如下图

list

点击提交按钮删除它们。

我这样删除了:

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

当然它并不起作用。我只是想将一些MVC示例重制为Razor Pages。也许你有更好的解决方案,但我真的卡在这里。

1 个答案:

答案 0 :(得分:0)

所以,是的,我刚刚在我的Index.cshtml页面上创建了一个按钮,它为Edit页面提供了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&#34; {id}&#34;中添加,通过上一页提供 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>