0

I have a controller class that I get data from database and return it in a function, Now I want to call this function in a js and set the data in variables to show in a page:

My code looks like: exampleController.cs

namespace iSee.WebApiHse.Controllers
{
    public class expController : StandardController
    {

        public expController(
            first myService,
            ISystemSettings systemSettings,
            IService myExpService)
        {
            _myService = MyService;
            _systemSettings = systemSettings;
            _myExpService = myExpService;
        }

        // GET data
        public ActionResult Myexample(int id)
        {
            var elementIds = _systemSettings.ExpIds; 

            var myElements = CacheService.AllVisibleElements
                                  .Where(x => elementIds.Contains(x.Id)).ToList();

            var container = _kpiContainerService.Find(id);

            var result = _myService.MonthByContainer(myElements, container);
            return AsJson(result);
        }
    }
}

This works and I get the data. Now I have myExp.js that I need to use these data in it. How can I do that?

Thanks

5 Answers 5

2

You need to execute $ajax(..) (jquery syntax) request to your controller to pass and get compute information from the server.

For this your controller method, that you're going to call, has to be exposed for HTTP access.

More details on :

How to call controller method from javascript

Sign up to request clarification or add additional context in comments.

Comments

0

Do you want work with your controller in View by JavaScript? It isn't good idea. You should pass Model to View and work with it or ajax and recieve json-data

Comments

0

An example that uses jQuery-ajax to call your C# method:

// javascript

var id = 987;

$.ajax({
    url : '/expController/Myexample/',
    type : 'GET',
    data { id : id },
    dataType : 'json', // <-- tell jQuery to decode the JSON automatically
    success : function(response){
        console.log(response);
    }
});

This would call your method, passing in the value of id, then it would decode the JSON into the response object.

For plain Javascript ajax (no jQuery) see MDN Ajax.

Comments

0

You need to make a request to the Action Myexample. Usually this is done via AJAX:

In your view you could have:

function makeAJaxCall(idToSend) {
    $.ajax({
        url: '@Url.Action("exp", "Myexample")',
        data: { id : idToSend },
        type: "POST",
        success: function(data) {
           $("#HTMLElement").val(data.YourData);
        }
    });
}

Your response will come back in the data variable if the AJAX call succeeds. I have provided an example to show you how to change the value of an HTML element with the ID HTMLElement.

To invoke the function you can do:

 makeAjaxCall(100);

Comments

0

You may load the JSON data in a var and pass it to the function in expjs.js, as:

var data = data_from_ajax();
exp_js_function(data);

data_from_ajax() would receive JSON data from your controller and action method.

Please consider this as a starting point and not as a copy-paste solution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.