用户列表预览
Controllers->AccountController.cs
[HttpGet]
public IActionResult Index()
{
return View(_userManager.Users);
}
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
Views->Account->Index.cshtml
@model IEnumerable<ApplicationUser>
@{
ViewBag.Title = "Index";
} <div class="panel panel-primary">
<div class="panel-heading">
User Accounts
</div>
<table class="table table-striped"> <tr><th>ID</th><th>Name</th><th>Email</th></tr>
@if (Model.Count() == ) {
<tr><td colspan="" class="text-center">No User Accounts</td></tr>
} else {
foreach (ApplicationUser user in Model) {
<tr>
<td>@user.Id</td>
<td>@user.UserName</td>
<td>@user.Email</td>
<td>
@using (Html.BeginForm("Delete", "Account",
new { id = user.Id })) {
@Html.ActionLink("Edit", "Edit", new { id = user.Id },
new { @class = "btn btn-primary btn-xs" })
<button class="btn btn-danger btn-xs" type="submit">
Delete
</button>
}
</td>
</tr>
}
}
</table>
</div>
@Html.ActionLink("Create", "Create", null, new { @class = "btn btn-primary" })
删除
[HttpPost]
public async Task<ActionResult> Delete(string id) {
ApplicationUser user = await _userManager.FindByIdAsync(id);
if (user != null) {
IdentityResult result = await _userManager.DeleteAsync(user);
if (result.Succeeded) {
return RedirectToAction("Index");
} else {
return View("Error", result.Errors);
}
} else {
return View("Error", new string[] { "User Not Found" });
}
}
修改
Controllers->AccountController.cs
[HttpGet]
public async Task<ActionResult> Edit(string id) {
ApplicationUser user = await _userManager.FindByIdAsync(id);
if (user != null) {
return View(user);
} else {
return RedirectToAction("Index");
}
} [HttpPost]
public async Task<ActionResult> Edit(string id, string email, string password) {
ApplicationUser user = await _userManager.FindByIdAsync(id);
if (user != null) {
user.PasswordHash =_userManager.PasswordHasher.HashPassword(user,password);
IdentityResult result = await _userManager.UpdateAsync(user);
if (result.Succeeded) {
return RedirectToAction("Index");
} else {
AddErrors(result);
}
} else {
ModelState.AddModelError("", "User Not Found");
}
return View(user);
}
Views->Account->Edit.cshtml
@model ApplicationUser
@{ ViewBag.Title = "Edit"; }
@Html.ValidationSummary(false)
<h2>Edit User</h2> <div class="form-group">
<label>Name</label>
<p class="form-control-static">@Model.Id</p>
</div>
@using (Html.BeginForm()) {
@Html.HiddenFor(x => x.Id)
<div class="form-group">
<label>Email</label>
@Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Password</label>
<input name="password" type="password" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">Save</button>
@Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-default" })
}
Views ->Shared->_Layout.cshtml 加一个主跳转连接
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
<li><a asp-area="" asp-controller="Account" asp-action="Index">Account</a></li> //加这句