ASP.NET MVC 5 calling controller method from Javascript Error

jhinx

I am attempting to call a controller method from javascript and I seem to be having trouble getting this to execute correctly. I have very little experience with javascript and have followed other examples of how to do this from stackoverflow but I am still having some issues- if anyone can help that'd be fantastic.

Basically what I am trying to do is set a .data tag on a javascript object to the string returned by a method on the controller (this method calls a webservice that runs a SQL Server function). The method needs to be passed one parameter which is used in the function.

The code is below:

Javascript Code

for (var i = 0; i < stats.length; i++)
{ 
    var stat = stats[i].data('id');
    var color = CallService(stat);
    this.node.fill = color;
}

JQuery Method

    function CallService(id) {
    $.ajax({
        url: '@Url.Action("CallService", "NodeController")',
        type: 'GET',
        dataType: 'json',
        cache: false,
        data: { 'id': id },
        success: function (color) {
            return color;
        },
        error: function () {
            alert('Error occured');
        }
    });
} 

Controller Method

    [HttpGet]
    public JsonResult CallService(string id)
    {
        var idNum = Convert.ToInt32(id);
        var StationService = new getStationStatus.Service1SoapClient("Service1Soap");
        string color = StationService.getStationStatus(idNum);
        return Json(color, JsonRequestBehavior.AllowGet);
    }

the controller is called the NodeController- which is what I am referring to in url: call of ajax.

Basically what is happening is when i run the page, I first get an error saying it cannot set this.node.fill to a null value THEN I get the alert that an error occurred- like I said I am pretty inexperienced with javascript so I am honestly not even sure if it is calling the method in the correct order if i get an error on this.node.fill before I receive the error message from JQuery.

Any/all help or suggestions is greatly appreciated!!!

Igor
  1. If your controller class is NodeController, use only "Node" for the controller name in Url.Action:

    url: '@Url.Action("CallService", "Node")',
    
  2. You cannot synchronously return a value from an asynchronous function. Either pass a callback to CallService to be called on success or use jquery promise - http://api.jquery.com/promise/

  3. We don't know what this.node.fill is or where it is defined. Likely, this.node is not defined at the time the assignment is executed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Asp.net MVC 5 not calling controller create method

From Dev

Calling ASP.NET MVC 4 Controller from Javascript

From Dev

Calling the controller correctly from JavaScript - ASP.NET MVC

From Dev

Calling Classic ASP from ASP.NET MVC5 Controller on the same server

From Dev

Asp.net MVC Ajax call not calling controller method

From Dev

Asp.net MVC Ajax call not calling controller method

From Dev

ASP Net MVC calling function in controller from JS errors

From Dev

ASP.Net MVC keeps calling the index action method for each controller action method I call

From Dev

Inheritance from Controller in Asp.NET MVC 5

From Dev

ASP.NET MVC calling server from javascript function with ajax

From Dev

Calling async method from MVC controller

From Dev

Calling async method from MVC controller

From Dev

Calling a method in Razor ASP.NET MVC

From Dev

Calling a method in Razor ASP.NET MVC

From Dev

ASP.NET MVC calling a method

From Dev

calling action method with javascript asp mvc

From Dev

Calling another different view from the controller using ASP.NET MVC 4

From Dev

Calling View of different folder from Asp.net mvc4 controller

From Dev

Calling View of different folder from Asp.net mvc4 controller

From Dev

Cannot await 'long' when calling an async method inside asp.net mvc5

From Dev

Weird Error Upgrading ASP.NET MVC from 4 to 5

From Dev

ASP.NET MVC 5: single controller method to handle paths in file browser mode

From Dev

Sending data by using AngularJS to an action method of ASP.NET MVC 5 controller

From Dev

Angular asp.net mvc calling c# controller function

From Dev

Angularjs in Asp.net MVC project calling web api controller

From Dev

MVC Calling ActionResult Method from JavaScript

From Dev

POST method calling from service Angular 6 to the REST using web api in asp.net mvc

From Dev

ASP.NET MVC5 Elmah Error Log/Email The controller for path was not found

From Dev

ASP.NET MVC - JSON.NET extension method for controller

Related Related

  1. 1

    Asp.net MVC 5 not calling controller create method

  2. 2

    Calling ASP.NET MVC 4 Controller from Javascript

  3. 3

    Calling the controller correctly from JavaScript - ASP.NET MVC

  4. 4

    Calling Classic ASP from ASP.NET MVC5 Controller on the same server

  5. 5

    Asp.net MVC Ajax call not calling controller method

  6. 6

    Asp.net MVC Ajax call not calling controller method

  7. 7

    ASP Net MVC calling function in controller from JS errors

  8. 8

    ASP.Net MVC keeps calling the index action method for each controller action method I call

  9. 9

    Inheritance from Controller in Asp.NET MVC 5

  10. 10

    ASP.NET MVC calling server from javascript function with ajax

  11. 11

    Calling async method from MVC controller

  12. 12

    Calling async method from MVC controller

  13. 13

    Calling a method in Razor ASP.NET MVC

  14. 14

    Calling a method in Razor ASP.NET MVC

  15. 15

    ASP.NET MVC calling a method

  16. 16

    calling action method with javascript asp mvc

  17. 17

    Calling another different view from the controller using ASP.NET MVC 4

  18. 18

    Calling View of different folder from Asp.net mvc4 controller

  19. 19

    Calling View of different folder from Asp.net mvc4 controller

  20. 20

    Cannot await 'long' when calling an async method inside asp.net mvc5

  21. 21

    Weird Error Upgrading ASP.NET MVC from 4 to 5

  22. 22

    ASP.NET MVC 5: single controller method to handle paths in file browser mode

  23. 23

    Sending data by using AngularJS to an action method of ASP.NET MVC 5 controller

  24. 24

    Angular asp.net mvc calling c# controller function

  25. 25

    Angularjs in Asp.net MVC project calling web api controller

  26. 26

    MVC Calling ActionResult Method from JavaScript

  27. 27

    POST method calling from service Angular 6 to the REST using web api in asp.net mvc

  28. 28

    ASP.NET MVC5 Elmah Error Log/Email The controller for path was not found

  29. 29

    ASP.NET MVC - JSON.NET extension method for controller

HotTag

Archive