/// <summary> /// 删除实体 /// </summary> /// <param>实体</param> /// <param>是否立即保存</param> /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns> public int Delete(T entity, bool isSave) { DbContext.Set<T>().Attach(entity); DbContext.Entry<T>(entity).State = EntityState.Deleted; return isSave ? DbContext.SaveChanges() : 0; }
打开AdminController 添加DeleteJson(List<int> ids)方法
// <summary> /// 删除 /// Response.Code:1-成功,2-部分删除,0-失败 /// Response.Data:删除的数量 /// </summary> /// <returns></returns> [HttpPost] public JsonResult DeleteJson(List<int> ids) { int _total = ids.Count(); Response _res = new Core.Types.Response(); int _currentAdminID = int.Parse(Session["AdminID"].ToString()); if (ids.Contains(_currentAdminID)) { ids.Remove(_currentAdminID); } _res = adminManager.Delete(ids); if(_res.Code==1&& _res.Data < _total) { _res.Code = 2; _res.Message = "共提交删除"+_total+"名管理员,实际删除"+_res.Data+"名管理员。\n原因:不能删除当前登录的账号"; } else if(_res.Code ==2) { _res.Message = "共提交删除" + _total + "名管理员,实际删除" + _res.Data + "名管理员。"; } return Json(_res); }
在Index视图 script脚本区域,“//添加按钮结束”后面添加删除js代码
//添加按钮结束 //删除按钮 $("#btn_del").click(function () { var selected = $table.bootstrapTable('getSelections'); if ($(selected).length > 0) { BootstrapDialog.confirm("确定删除选中的" + $(selected).length + "位管理员", function (result) { if (result) { var ids = new Array($(selected).length); $.each(selected, function (index, value) { ids[index] = value.AdministratorID; }); $.post("https://www.jb51.net/@Url.Action("DeleteJson","Admin")", { ids: ids }, function (data) { if (data.Code != 0) { BootstrapDialog.show({ message: data.Message, buttons: [{ icon: "glyphicon glyphicon-ok", label: "确定", action: function (dialogItself) { $table.bootstrapTable("refresh"); dialogItself.close(); } }] }); } else BootstrapDialog.alert(data.Message); }, "json"); } }); } else BootstrapDialog.warning("请选择要删除的行"); }); //删除按钮结束
4、重置密码
在AdminController中 添加ResetPassword(int id)方法。方法中将密码重置为Ninesky。
/// <summary> /// 重置密码【Ninesky】 /// </summary> /// <param>管理员ID</param> /// <returns></returns> [HttpPost] public JsonResult ResetPassword(int id) { string _password = "Ninesky"; Response _resp = adminManager.ChangePassword(id, Security.SHA256(_password)); if (_resp.Code == 1) _resp.Message = "密码重置为:" + _password; return Json(_resp); }
在添加script代码中表格代码段可以看到,这里通过 连接的onclick调用ResetPassword方法,所以ResetPassword方法要放在表格生成前面,不然会出现 方法未定义的错误。
这里把代码放到$(document).ready的前面。
<script type="text/javascript"> //重置密码 function ResetPassword(id, accounts) { BootstrapDialog.confirm("确定重置" + accounts + "的密码", function (result) { if (result) { $.post("https://www.jb51.net/@Url.Action("ResetPassword", "Admin")", { id: id }, function (data) { BootstrapDialog.alert(data.Message); }, "json"); } }); }; //重置密码结束 $(document).ready(function () { //表格
5、修改管理员密码
在在AdminController中 添加MyInfo()方法。
/// <summary> /// 我的资料 /// </summary> /// <returns></returns> public ActionResult MyInfo() { return View(adminManager.Find(Session["Accounts"].ToString())); }
右键添加视图
@model Ninesky.Core.Administrator @{ ViewBag.Title = "我的资料"; } @section SideNav{@Html.Partial("SideNavPartialView")} <ol> <li><span></span> @Html.ActionLink("首页", "Index", "Home")</li> <li>@Html.ActionLink("管理员", "Index", "Admin")</li> <li>我的资料</li> </ol> @Html.Raw(ViewBag.Message) @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div> @Html.LabelFor(model => model.Accounts, htmlAttributes: new { @class = "control-label col-md-2" }) <div> @Html.DisplayTextFor(model => model.Accounts) </div> </div> <div> @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" }) </div> </div> <div> @Html.LabelFor(model => model.LoginIP, htmlAttributes: new { @class = "control-label col-md-2" }) <div> @Html.DisplayTextFor(model => model.LoginIP) </div> </div> <div> @Html.LabelFor(model => model.LoginTime, htmlAttributes: new { @class = "control-label col-md-2" }) <div> @Html.DisplayTextFor(model => model.LoginTime) </div> </div> <div> @Html.LabelFor(model => model.CreateTime, htmlAttributes: new { @class = "control-label col-md-2" }) <div> @Html.DisplayTextFor(model => model.CreateTime) </div> </div> <div> <div> <input type="submit" value="保存" /> </div> </div> </div> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
在在AdminController中 添加处理方法MyInfo(FormCollection form)方法。