SignalR-从视图获取数据的问题

时间:2018-09-17 11:10:16

标签: jquery .net asp.net-mvc asp.net-web-api signalr

我用SignalR创建了Web Api,以从数据库对象中获取数据作为View。当我更新与视图连接的表时,数据未获取,并且html页面中未显示更新的数据。我们在第一个解决方案中创建了两个单独的解决方案,我们有api和MyHub类来调用客户端函数displayStatus。在第二个解决方案中,我们具有html和displayStatus()函数。无法从另一个解决方案(即第一个解决方案)中的MyHub类调用displayStatus()函数。

我的代码: ApiController:

using SignalRMVC; namespace SignalRWebAPI.Controllers { public class MyController : ApiController { [Route("api/GetData")] public string GetData(string cri="") { var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStrSignalR"].ConnectionString); string query = @"SELECT (*my query comes here*)"; connection.Open(); SqlCommand cmd = new SqlCommand(query, connection);

        cmd.Notification = null;
        SqlDependency.Start(ConfigurationManager.ConnectionStrings["ConStrSignalR"].ConnectionString);
        SqlDependency dependency = new SqlDependency(cmd);
        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

        if (connection.State == ConnectionState.Closed)
            connection.Open();
        var reader = cmd.ExecuteReader();
        var result = reader.Cast<IDataRecord>()
                        .Select(x => new PropertyClassName()
                        {
                            property1 = x.GetValue(0).ToString(),
                            property1 = x.GetValue(1).ToString(),
                            .
                            .
                            .
                        }).ToList();

        string jsonResult = JsonConvert.SerializeObject(result);

        return jsonResult;
    }
    private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        MyHubMvc.Show();
    }
}

集线器类别: using Microsoft.AspNet.SignalR; namespace SignalRMVC { public class MyHubMvc : Hub { public static void Show() { IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHubMvc>(); context.Clients.All.displayStatus(); } } }
脚本:

 <script type="text/javascript">
    $(function () {
        $.connection.myHubMvc.client.displayStatus = function () {
            getData();
        };
        $.connection.hub.start();
        getData();
    });

    function getData() {
        var $tbl = $('#tbl');
        var crit = $('#ddlTypes').val();
        $.ajax({
            url: 'http://localhost:.../api/GetData',
            data: { 'cri': crit },
            dataType: "json",
            success: function (data) {
                data = JSON.parse(data);
                var tbl = '<table class="w3-table-all w3-hoverable">';
                tbl += '<tr><th>property1</th><th>property2</th>....<tr>';
                $tbl.empty();
                for (var i = 0; i < data.length; i++) {
                    tbl += '<tr class="w3-light-grey">';
                    tbl += '<td>' + data[i].property1 + '</td>';
                    tbl += '<td>' + data[i].property2 + '</td>';
                    .
                    .
                    .                      
                    tbl += '</tr>';
                }
                tbl += '</table>';
                $tbl.html(tbl);
            },
        })
    }
    </script>

0 个答案:

没有答案