As soon as my page loads I would like to make a web request so that its response can be accessed via JavaScript. I have written a c# class that makes the requests to the server. This class is invoked by a controller in my MVC application. I can confirm that the client class is functioning properly & returns a string representation of the JSON.

My question is this. How should I call the controller so that I can use the response in my javascript? The @http.Action simply returns a txt file containing the html of the whole page with the response in the place that i make the call. I would like to instead store this as a variable and maintaining the initial Index view..

Code Bellow has been modified to only include essentials.


    public ActionResult getMeterConfig () {
        string scheme = "http";
        string host = "*****";
        int port = ****;
        string dataset = "*****";
        string db = "****";
        string method = "GET";

        var request = new client();
        request.setProps(scheme, host, port, dataset, db, method);
        var response = request.getResponse();

        return Json (response,JsonRequestBehavior.AllowGet);


Then to call the controller on page load:

    var json = @Html.Action("actionName","contollerName";

EDIT For Clarification

Ive changed the javascript to be :

    $(window).load(function () {
        $.get('@Html.Action("getMeterConfig", "GetMeterConfig")', function (response) {
            var meters = response;

This returns a download entitled Index.json rather than store the response in the variable meters.

enter image description here

To make this even more peculiar the .json document reads as follows... It includes my index view HTML and substitutes the @Html.Action("getMeterConfig", "GetMeterConfig") with the actual response.

enter image description here

Related posts

Recent Viewed