Search code examples
c#jsonajaxasp.net-mvcjsonresult

failed to return two parameters from jsonresult in .net 5 mvc to ajax


i have problem to return two parameters result from return json in c# .net 5 to ajax. where the return result cannot be read in ajax

i have html code

<button id="DELETE" onclick="ConfirmDelete(@item.Id)" class="btn btn-danger btn-sm">Delete</button>

jquery code

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();
                            }
                        });    
                        
                    }
                });

            }

        })
    };

controller

[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});
        }

but if I pass only one parameter, then the return result can be read fine in ajax

controller

return Json("Success");

jquery code

success: function (data) {
                        if (data == "Success") {
                            Swal.fire({
                                icon: 'success',
                                title: 'Delete Success',
                                text: '',
                                confirmButtonText: 'Ok'
                            }).then((result) => {
                                if (result.isConfirmed) {
                                    location.reload();
                                }
                            });
                        }

can anyone help why this is different, and what should i fix? thank you


Solution

  • try this

    return new JsonResult( new {IsSuccess = result, MessageError = msgerror} );