简体   繁体   English

在visual studio中用javascript调用一个类

[英]Call a class in javascript in visual studio

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: 我有一个控制器类,我从数据库中获取数据并将其返回到函数中,现在我想在js中调用此函数并将数据设置在变量中以显示在页面中:

My code looks like: exampleController.cs 我的代码看起来像: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. 现在我有myExp.js ,我需要在其中使用这些数据。 How can I do that? 我怎样才能做到这一点?

Thanks 谢谢

You need to execute $ajax(..) (jquery syntax) request to your controller to pass and get compute information from the server. 您需要向控制器执行$ajax(..) (jquery语法)请求以从服务器传递和获取计算信息。

For this your controller method, that you're going to call, has to be exposed for HTTP access. 为此,必须公开您要调用的控制器方法以进行HTTP访问。

More details on : 更多细节:

How to call controller method from javascript 如何从javascript调用控制器方法

Do you want work with your controller in View by JavaScript? 你想在JavaScript中查看你的控制器吗? It isn't good idea. 这不是个好主意。 You should pass Model to View and work with it or ajax and recieve json-data 您应该将Model传递给View并使用它或ajax并接收json数据

An example that uses jQuery-ajax to call your C# method: 使用jQuery-ajax调用C#方法的示例:

// 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. 这将调用您的方法,传入id的值,然后它将JSON解码为response对象。

For plain Javascript ajax (no jQuery) see MDN Ajax . 对于普通的Javascript ajax(没有jQuery),请参阅MDN Ajax

You need to make a request to the Action Myexample . 您需要向Action Myexample发出请求。 Usually this is done via AJAX : 通常这是通过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. 如果AJAX调用成功,您的响应将返回到data变量中。 I have provided an example to show you how to change the value of an HTML element with the ID HTMLElement. 我提供了一个示例,向您展示如何使用ID HTMLElement更改HTML元素的值。

To invoke the function you can do: 要调用该功能,您可以执行以下操作:

 makeAjaxCall(100);

You may load the JSON data in a var and pass it to the function in expjs.js , as: 您可以在var加载JSON数据并将其传递给expjs.js的函数,如下所示:

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

data_from_ajax() would receive JSON data from your controller and action method. data_from_ajax()将从您的控制器和操作方法接收JSON数据。

Please consider this as a starting point and not as a copy-paste solution 请将此视为起点,而不是复制粘贴解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM