ASP.NET Identity教程二:(用户管理4)删除用户
后台管理肯定要删除用户。
1.在用户列表前台进行修改。
打开User下的List前台。List.cshtml,修改最后的删除按钮,增加一个弹窗确认。
@Html.ActionLink("删除", "Del", new { id = user.Id }, new { @class = "btn btn-warning" })
显示效果为

2.在User控制器中增加删除方法
// GET: Menus/Delete/5
public ActionResult Del(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
}
User user = UserManager.FindById(id);
return View(user);
}
// POST: Menus/Delete/5
[HttpPost, ActionName("Del")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(string id)
{
User user = UserManager.FindById(id);
await UserManager.DeleteAsync(user);
return RedirectToAction("List","User");
}
3.增加一个Del视图。
@model jsdhh2.Models.User
@{
ViewBag.Title = "Delete";
}
<h3>确定删除下列用户吗?</h3>
<br/>
<h3> @Html.DisplayNameFor(model => model.UserName)</h3><br /><br />
<div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-warning" /> |
@Html.ActionLink("不删除", "List",new { }, new { @class = "btn btn-success" })
</div>
}
</div>
5.查看删除效果。

点删除就删除后返回LIST,不删除便直接返回List
浙公网安备 33010602011771号