Action in controller
Method GetById is the same as the Find method and EntityUpdate == Update
public IActionResult CategoryStatus(int id)
{
var data = cm.GetById(id);
if (data.CategoryStatus)
{
data.CategoryStatus = false;
}
else
{
data.CategoryStatus = true;
}
cm.EntityUpdate(data);
return RedirectToAction("Index");
}
I want to call this action from razor view with ajax, can anyone help , thanks.
I add some parts of razor view just in case
<a href="/Admin/Category/CategoryStatus/@item.CategoryID"
onclick="return functionConfirm(this)">
Aktivləşdir
</a>
<script>
function functionConfirm(event) {
swal({
title: 'Are you sure?',
text: "You will not be able to recover this item!",
type: 'warning',
showCancelButton: true,
cancelButtonText: 'No',
cancelButtonClass: 'btn btn-danger',
showConfirmButton: true,
confirmButtonText: 'Yes',
confirmButtonClass: 'btn btn-success'
}, function (isConfirm) {
if (isConfirm) {
$.ajax({//I need your help here
type: "GET",
url: '/Admin/Category/CategoryStatus',
success: function (msg) {
console.log(msg);
},
error: function (req, status, error) {
console.log(msg);
}
});
return true;
} else {
return false;
}
});
return false;
}
CodePudding user response:
Change your code like below:
//add this....
<a data-id="@item.CategoryID"
onclick="return functionConfirm()">
Aktivləşdir
</a>
@section Scripts
{
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" integrity="sha512-gOQQLjHRpD3/SEOtalVq50iDn4opLVup2TF8c4QPI3/NmUPNZOk2FG0ihi8oCU/qYEsw4P6nuEZT2lAG0UNYaw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js" integrity="sha512-7VTiy9AhpazBeKQAlhaLRUk kAMAb8oczljuyJHPsVPWox/QIXDFOnT9DUk1UC8EbnHKRdQowT7sOBe7LAjajQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
function functionConfirm() {
swal({
title: 'Are you sure?',
text: "You will not be able to recover this item!",
type: 'warning',
showCancelButton: true,
cancelButtonText: 'No',
cancelButtonClass: 'btn btn-danger',
showConfirmButton: true,
confirmButtonText: 'Yes',
confirmButtonClass: 'btn btn-success'
}, function (isConfirm) {
var id = $(event).data("id"); //add....
if (!isConfirm) return;
$.ajax({
type: "GET",
url: '/Admin/Category/CategoryStatus/' id, //modify...
success: function (msg) {
window.location.href="/xxx/Index"
},
error: function (req, status, error) {
console.log(msg);
}
});
});
}
</script>
}
CodePudding user response:
<a href="/Admin/Category/CategoryStatus/@item.CategoryID"
onclick="return functionConfirm(this)">
Aktivləşdir
</a>
<script>
var lots_of_stuff_already_done = false;
function functionConfirm(event) {
if (lots_of_stuff_already_done) {
lots_of_stuff_already_done = false; // reset flag
return; // let the event bubble away
}
swal({
title: 'Are you sure?',
text: "You will not be able to recover this item!",
type: 'warning',
showCancelButton: true,
cancelButtonText: 'No',
cancelButtonClass: 'btn btn-danger',
showConfirmButton: true,
confirmButtonText: 'Yes',
confirmButtonClass: 'btn btn-success'
}, function (isConfirm) {
if (isConfirm) {
// do lots of stuff
lots_of_stuff_already_done = true; // set flag
$(this).trigger('click');
return false;
}
});
}