在我的项目中,我使用datatable
进行crud操作。但是我有一个问题有时候没有成功的功能,当然,我看到我收到的网络标签{ status":true}
但{ {1}}没有重新加载,我重定向到空白页面。我已将警报功能添加到成功功能,但我的警报功能无法正常工作。有时我想删除一个错误datatable
从表中记录。有时The required anti-forgery form field "__RequestVerificationToken" is not present.
和create
会给我edit
消息。
{ status":true}
==========================
// GET: Admin/Users/Delete/5
public ActionResult Delete(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.User.Find(id);
UserViewModel userViewModel=new UserViewModel();
userViewModel.UserId = user.Id;
userViewModel.UserName = user.UserName;
userViewModel.FirstName = user.FirstName;
userViewModel.LastName = user.LastName;
userViewModel.Email = user.Email;
userViewModel.IsActive = user.IsActive;
userViewModel.Gender = user.Gender;
userViewModel.PhoneNumber = user.PhoneNumber;
userViewModel.Address = user.Address;
if (user == null)
{
return HttpNotFound();
}
return PartialView(userViewModel);
}
// POST: Admin/Users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(string id)
{
bool status=false;
User user = db.User.Find(id);
db.User.Remove(user);
db.SaveChanges();
return Json(new { status = true }, JsonRequestBehavior.AllowGet);
}
===================================
@model IEnumerable<Dentistry.Areas.ViewModels.UserViewModel>
@{
ViewBag.Title = "مدیریت کاربران";
}
@*<h2>مدیریت کاربران</h2>*@
@section Styles{
<link href="~/Areas/admin/assets/global/plugins/datatables/datatables.min.css" rel="stylesheet" type="text/css" />
<link href="~/Areas/admin/assets/global/plugins/datatables/plugins/bootstrap/datatables.bootstrap-rtl.css" rel="stylesheet" type="text/css" />
<link href="~/Areas/admin/assets/global/plugins/bootstrap-modal/css/bootstrap-modal-bs3patch.css" rel="stylesheet" type="text/css" />
<link href="~/Areas/admin/assets/global/plugins/bootstrap-modal/css/bootstrap-modal.css" rel="stylesheet" type="text/css" />
}
<style>
.table-checkable tr > td:first-child, .table-checkable tr > th:first-child {
text-align: center;
max-width: 50000px;
min-width: 40px;
padding-right: 0;
padding-left: 0;
}
#delete .modal-header {
border-bottom: 1px solid #EFEFEF;
background: #e7505a !important;
}
.modal {
border-radius: 0px;
-webkit-border-radius: 0px;
-moz-border-radius: 0px;
}
</style>
<div class="col-md-12 col-sm-12">
<!-- BEGIN EXAMPLE TABLE PORTLET-->
<div class="portlet box green">
<div class="portlet-title">
<div class="caption">
<i class="fa fa-cogs"></i>کاربران
</div>
<div class="actions">
<a href="/Admin/Users/Create" data-target="#create" data-toggle="modal" class="btn btn-default btn-sm">
<i class="fa fa-plus"></i> کاربر جدید
</a>
<a href="javascript:window.print()" class="btn btn-default btn-sm">
<i class="fa fa-print"></i> چاپ
</a>
</div>
</div>
<div class="portlet-body">
<table class="table table-striped table-bordered table-hover table-checkable order-column myDatatable">
<thead>
<tr>
@*<th class="table-checkbox">
<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
<input type="checkbox" class="group-checkable" data-set="myDatatable .checkboxes" />
<span></span>
</label>
</th>*@
<th>نام کاربری </th>
<th>نام </th>
<th>نام خانوادگی</th>
<th>جنسیت</th>
<th>ایمیل</th>
<th>وضعیت</th>
<th>شماره تماس</th>
<th>آدرس</th>
<th>ویرایش</th>
<th>حذف</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
<div id="edit" class="modal fade" tabindex="-1" data-width="760">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h4 class="modal-title">ویرایش کاربران</h4>
</div>
<div class="modal-body">
<div class="row">
</div>
</div>
</div>
<div id="delete" class="modal fade" tabindex="-1" data-width="760">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h4 class="modal-title">آیا قصد حذف این کاربر را دارید؟</h4>
</div>
<div class="modal-body">
<div class="row">
</div>
</div>
</div>
<div id="create" class="modal fade" tabindex="-1" data-width="760">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h4 class="modal-title"><i class="fa fa-user-plus"></i> کاربر جدید </h4>
</div>
<div class="modal-body">
</div>
</div>
<!-- BEGIN Portlet PORTLET-->
<!-- END Portlet PORTLET-->
@section scripts{
<!-- BEGIN PAGE LEVEL PLUGINS -->
<script src="~/Areas/admin/assets/global/scripts/datatable.js" type="text/javascript"></script>
<script src="~/Areas/admin/assets/global/plugins/datatables/datatables.min.js" type="text/javascript"></script>
<script src="~/Areas/admin/assets/global/plugins/datatables/plugins/bootstrap/datatables.bootstrap.js" type="text/javascript"></script>
<script src="~/Areas/admin/assets/pages/scripts/table-datatables-managed.min.js" type="text/javascript"></script>
<!-- END PAGE LEVEL SCRIPTS -->
<script src="~/Areas/admin/assets/global/plugins/bootstrap-modal/js/bootstrap-modalmanager.js" type="text/javascript"></script>
<script src="~/Areas/admin/assets/global/plugins/bootstrap-modal/js/bootstrap-modal.js" type="text/javascript"></script>
<!-- BEGIN PAGE LEVEL SCRIPTS -->
<script src="~/Areas/admin/assets/pages/scripts/ui-extended-modals.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
var oTable = $('.myDatatable').DataTable({
"ajax": "/Users/GetUsers",
"language": {
"search": "جستجو:",
"lengthMenu": " تعداد _MENU_ رکورد در هر صفحه"
},
"autoWidth": true,
"columns": [
{ "data": "UserName", },
{ "data": "FirstName", },
{ "data": "LastName", },
{ "data": "Gender", },
{ "data": "Email", },
{ "data": "IsActive", },
{ "data": "PhoneNumber", },
{ "data": "Address", },
{
"data": "UserId", "width": "50px", "render": function (data) {
//
return '<a class="btn btn-info fa fa-pencil" data-toggle="modal" data-dismiss="modal" data-target="#edit" href="/Admin/Users/Edit/' + data + '"></a>';
}
},
{
"data": "UserId", "width": "50px", "render": function (data) {
return '<a class="btn btn-danger fa fa-trash" data-toggle="modal" data-dismiss="modal" data-target="#delete" href="/Admin/Users/Delete/' + data + '"></a>';
}
}
//}, {
// "data": 'IsActive',
// render: function (data, type, row) {
// return '<span class="label label-sm label-success"> ' + data + ' </span>'
// }
//}
], "columnDefs": [
{
"render": function (data, type, row) {
return '<span class="label label-sm label-success"> ' + data ? 'آقا' : 'خانم' + ' </span>' ;
},
"targets": 3 // replace 0 by your column if
}, {
"render": function (data, type, row) {
return data ? 'فعال' : 'غیرفعال';
},
"targets": 5
}
]
//"fnCreatedRow": function (row, data, dataIndex) {
// var div = document.createElement("span");
// $(row).children("td:nth-child(1)").append(div);
//}
});
$('body').on('hidden.bs.modal', '.modal', function () {
$(this).removeData('bs.modal');
});
});
</script>
}
答案 0 :(得分:1)
请删除此行
[ValidateAntiForgeryToken]
因为您没有发送防伪密钥。在ajax参数中发送防伪密钥或删除它。
用于发送在请求中添加此参数&#39; __ RequestVerificationToken&#39;使用其生成的值 var token = Html.AntiForgeryToken();