0

In my application in .NET Core (2.2) MVC, I am doing a get request to my controller with user input using ajax.

Here is the code:

 $.ajax({
            type: 'GET',
            url: "/BuildSheetsArea/TesterParameters/GetMassEditing",
            data: { 'ChangeTypeId': ChangeTypeId, 'param': param, 'ParameterValue': ParameterValue },
            dataType: 'json',
            success: function (data) {
                alert('success');
                console.log(data);

            },
            error: function (emp) {
                alert('error');
            }
        });

Image 01 from view page

I am getting hit to my controller successfully and my controller is returning the result.

Here is my controller code:

[HttpGet]
public System.Web.Mvc.JsonResult GetMassEditing(string ChangeTypeId, string param, string ParameterValue = "")
{
    List<TesterParameterMassEditingViewModel> finalList = new List<TesterParameterMassEditingViewModel>();

    // Code to perform necessary action

    return new System.Web.Mvc.JsonResult { Data = finalList, JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet };
}

Here is a screenshot where controller is returning the result successfully

Controller image

BUT on success I am not getting anything to my view. As you see in my ajax call on success I am trying to write the data from controller to console, but after controller returning the data nothing is happening. It never goes back to my ajax success function.

Am I missing anything? Kindly help.

I am using .NET Core 2.2 and Entity Framework Core 2.2.6

EDIT: Adding the network tab result screenshot here

network status

EDIT2: After @Rena's suggesting I have change my code. but Now I am having a new problem. From my controller when I am trying to do a DB query any in retun tries to pass the object my request goes to pending status. Explanation is below. here is my AXAJ request:

$.ajax({
            type: 'POST',
            url: "/BuildSheetsArea/TesterParameters/GetMassEditing",
            data: {'ChangeTypeId': ChangeTypeId, 'param': param, 'ParameterValue': ParameterValue },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                debugger;
                alert('success');
                console.log(data);
            },
            error: function (emp) {
                alert('error');
            },
        });
    });

And here is my controller code:

        [HttpPost]
    public async Task<JsonResult> GetMassEditing(string ChangeTypeId, string param, string ParameterValue)
    {
        List<TesterParameter> deviceList = await _context.TesterParameters.ToListAsync();
        string val = "See from console";
        return Json(deviceList);
    }

My Device list has 120 objects in it. This case in my network tab in browser I am getting this. My first request is in pending status and I am not getting anything in my ajax success method.

enter image description here

But if I donot do any db query and simply return a string, it works fine

        [HttpPost]
    public async Task<JsonResult> GetMassEditing(string ChangeTypeId, string param, string ParameterValue)
    {
        string val = "See from console";
        return Json(val);
    }

enter image description here

In 3rd case if I do the db query only but return the simple string, my network tab shows request in pending and in ajax success method I get nothing.

Please help.

user2019
  • 187
  • 1
  • 4
  • 16
  • If you remove `dataType: 'json'` does it work? – JEV Feb 18 '20 at 15:51
  • I have tried by removing datatype: 'json' . But still same. – user2019 Feb 18 '20 at 15:55
  • Do you see the `alert('success')`? Also check the state of the request in the network tab of dev tools. – Rory McCrossan Feb 18 '20 at 15:55
  • Hi, No I dint see that alert success :( and in my network tab it says status 200 for the request. I am adding the network tab screenshot to my post – user2019 Feb 18 '20 at 15:58
  • ok, one thing that might be wrong, you get a success even if it fails could you try to execute the request as a normal get in a browser window and check the response? – Isparia Feb 18 '20 at 16:02
  • Can you try changing the `JsonResult` to `List` and just adjust the return statement as such to `return finalList` – JEV Feb 18 '20 at 16:03
  • Not strictly related, but you probably shouldn't be sending a JSON payload to a GET method. It should be a POST, and you should be receiving a proper POCO object. – Ricardo Peres Feb 18 '20 at 16:10
  • That is because you use `system.web.mvc`,you could refer to:https://stackoverflow.com/questions/40912473/can-system-web-be-used-with-asp-net-core-with-full-framework – Rena Feb 19 '20 at 03:15

1 Answers1

0

If you are sure that you use asp.net core 2.2,you need to use Microsoft.AspNetCore.Mvc.JsonResult instead of System.Web.Mvc.JsonResult.

Change like below:

[HttpGet]
public Microsoft.AspNetCore.Mvc.JsonResult GetMassEditing(string ChangeTypeId, string param, string ParameterValue = "")
{
    List<TesterParameterMassEditingViewModel> finalList = new List<TesterParameterMassEditingViewModel>();

    // Code to perform necessary action

    return new JsonResult(finalList);
}

Result: enter image description here

Rena
  • 30,832
  • 6
  • 37
  • 72
  • Thank you so much. I have change my code. But now having a strange problem. I have Updated in my post under Edit2 . Kindly help – user2019 Feb 19 '20 at 14:04
  • You need to set breakpoint to `GetMassEditing` method(this line:`List deviceList = await _context.TesterParameters.ToListAsync();`).The reason why it is always pending is because you read much data from database,it needs long time to response.You need debug the server side code and wait for reading data from database then step to the `return Json(deviceList);`.No need to set debugger in js.Refer to [how to debug in vs](https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019). – Rena Feb 20 '20 at 07:33