未能 return 从 .net 5 mvc 中的 jsonresult 到 ajax 的两个参数
failed to return two parameters from jsonresult in .net 5 mvc to ajax
我有问题 return 两个参数从 return json 在 c# .net 5 到 ajax 中产生。在 ajax
中无法读取 return 结果
我有html代码
<button id="DELETE" onclick="ConfirmDelete(@item.Id)" class="btn btn-danger btn-sm">Delete</button>
jquery代码
function ConfirmDelete(id) {
Swal.fire({
icon:'question',
title: 'are you sure delete it?',
showCancelButton: true,
confirmButtonText: 'Ya',
confirmButtonColor: '#d33',
cancelButtonText: 'Tidak'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
type: 'POST',
url: '@Url.Content("Latihan/Delete")',
data: { Id: id },
dataType: "json",
success: function (data) {
if (data.Isuccess == true) {
Swal.fire({
icon: 'success',
title: 'Delete Success',
text: '',
confirmButtonText: 'Ok'
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
}
else {
Swal.fire({
icon: 'error',
title: 'Error Found',
text: data,
confirmButtonText: 'Ok'
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
}
},
error: function (data) {
Swal.fire({
icon: 'error',
title: 'Unknown Error',
text: 'Delete Failed',
confirmButtonText: 'Ok'
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
}
});
}
})
};
控制器
[HttpPost]
public async Task<JsonResult> Delete(int? id)
{
List<string> msgerror = new List<string>();
bool result = false;
try
{
if (id == null || id < 0)
{
msgerror.Add("Data cannot be null");
}
else
{
var LatihanDelete = await _context.TB_BIODATA.FirstOrDefaultAsync(e => e.Id == id);
if (LatihanDelete == null)
{
msgerror.Add("Data not found");
}
else
{
//_context.TB_BIODATA.Remove(LatihanDelete);
//await _context.SaveChangesAsync();
result = true;
}
}
}
catch (Exception e)
{
msgerror.Add("Error Exception : " + e.Message);
}
return Json(new{Isuccess = result, MessageError = msgerror});
}
但如果我只传递一个参数,那么 return 结果可以在 ajax
中正常读取
控制器
return Json("Success");
jquery代码
success: function (data) {
if (data == "Success") {
Swal.fire({
icon: 'success',
title: 'Delete Success',
text: '',
confirmButtonText: 'Ok'
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
}
任何人都可以帮助为什么这是不同的,我应该解决什么问题?谢谢
试试这个
return new JsonResult( new {IsSuccess = result, MessageError = msgerror} );
我有问题 return 两个参数从 return json 在 c# .net 5 到 ajax 中产生。在 ajax
中无法读取 return 结果我有html代码
<button id="DELETE" onclick="ConfirmDelete(@item.Id)" class="btn btn-danger btn-sm">Delete</button>
jquery代码
function ConfirmDelete(id) {
Swal.fire({
icon:'question',
title: 'are you sure delete it?',
showCancelButton: true,
confirmButtonText: 'Ya',
confirmButtonColor: '#d33',
cancelButtonText: 'Tidak'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
type: 'POST',
url: '@Url.Content("Latihan/Delete")',
data: { Id: id },
dataType: "json",
success: function (data) {
if (data.Isuccess == true) {
Swal.fire({
icon: 'success',
title: 'Delete Success',
text: '',
confirmButtonText: 'Ok'
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
}
else {
Swal.fire({
icon: 'error',
title: 'Error Found',
text: data,
confirmButtonText: 'Ok'
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
}
},
error: function (data) {
Swal.fire({
icon: 'error',
title: 'Unknown Error',
text: 'Delete Failed',
confirmButtonText: 'Ok'
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
}
});
}
})
};
控制器
[HttpPost]
public async Task<JsonResult> Delete(int? id)
{
List<string> msgerror = new List<string>();
bool result = false;
try
{
if (id == null || id < 0)
{
msgerror.Add("Data cannot be null");
}
else
{
var LatihanDelete = await _context.TB_BIODATA.FirstOrDefaultAsync(e => e.Id == id);
if (LatihanDelete == null)
{
msgerror.Add("Data not found");
}
else
{
//_context.TB_BIODATA.Remove(LatihanDelete);
//await _context.SaveChangesAsync();
result = true;
}
}
}
catch (Exception e)
{
msgerror.Add("Error Exception : " + e.Message);
}
return Json(new{Isuccess = result, MessageError = msgerror});
}
但如果我只传递一个参数,那么 return 结果可以在 ajax
中正常读取控制器
return Json("Success");
jquery代码
success: function (data) {
if (data == "Success") {
Swal.fire({
icon: 'success',
title: 'Delete Success',
text: '',
confirmButtonText: 'Ok'
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
}
任何人都可以帮助为什么这是不同的,我应该解决什么问题?谢谢
试试这个
return new JsonResult( new {IsSuccess = result, MessageError = msgerror} );