MVC4 Website Making Tutorial Chapter 3 Delete User Group Action 3.4
- 2021-08-12 02:32:38
- OfStack
1. Users
2. User groups
2.1 Browse user groups
2.2 Add User Groups
2.3 Modify user groups
2.4 Delete a user group
Deleting user groups is relatively simple. You don't need a separate page. When you browse the page and click Delete, a dialog box for confirming deletion will pop up. Click OK and delete it with jquery post.
Open "UserGroupController" and delete public ActionResult Delele (int GroupId) {return View ();}
Modify and delete processing Action [Delete (int Id)], modified code
/// <summary>
/// Delete a user group
/// </summary>
/// <param name="Id"> User group Id</param>
/// <returns></returns>
[HttpPost]
[AdminAuthorize]
public JsonResult Delete(int Id)
{
userGroupRsy = new UserGroupRepository();
if (userGroupRsy.Delete(Id)) return Json(true);
else return Json(false);
}
Here, the return type is JsonResult for easy post deletion using jquery.
Open List. cshtml and change @ Html. ActionLink ("delete", "Delete", new {id = item. UserGroupId}) to read
<
a href="javascript:void(0)" onclick="Delete(@item.UserGroupId,'@item.Name')"
>
Delete
<
/a
>
Add scripts at the bottom of the file
function Delete(id,name) {
if (confirm(" Are you sure you want to delete " " + name + " "Is it? ")) {
$.post("@Url.Content("~/UserGroup/Delete")", {Id:id}, function (data) {
if (data) {
alert(" Delete " " + name + " "Success! ");
location.reload();
}
});
}
}
When completed, the code for the entire List. cshtml is as follows:
@model IEnumerable<Ninesky.Models.UserGroup>
@{
ViewBag.Title = " User group list ";
Layout = "~/Views/Layout/_Manage.cshtml";
}
<div class="left">
<div class="top"></div>
Left side list
</div>
<div class="split"></div>
<div class="workspace">
<div class="inside">
<div class="notebar">
<img alt="" src="~/Skins/Default/Manage/Images/UserGroup.gif" /> User group list
</div>
<div class="buttonbar">@Html.ActionLink(" Add a user group ", "Add", "UserGroup") User group type:
@Html.DropDownList("GroupTypeList")
</div>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Type)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Type)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.ActionLink(" Modify ", "Edit", new { id = item.UserGroupId }) |
<a href="javascript:void(0)" onclick="Delete(@item.UserGroupId,'@item.Name')" > Delete </a>
</td>
</tr>
}
</table>
</div>
</div>
<div class="clear"></div>
<script type="text/javascript">
$("#GroupTypeList").change(function () {
window.location.href = "/UserGroup/List/" + $(this).children("option:selected").val();
})
function Delete(id,name) {
if (confirm(" Are you sure you want to delete " " + name + " "Is it? ")) {
$.post("@Url.Content("~/UserGroup/Delete")", {Id:id}, function (data) {
if (data) {
alert(" Delete " " + name + " "Success! ");
location.reload();
}
});
}
}
</script>
Finish work here!