当我尝试从服务器发送数据到客户端时没有发生任何事

时间:2012-12-10 20:24:30

标签: c# ajax asp.net-mvc-3 signalr

当我尝试在发布模式下执行此操作时,没有什么可以开心的,但是在调试模式下一切正常 - 为什么???

当我添加按钮并通过单击此按钮输出数据时。我的列表行中的内部链接也正常工作(http://clip2net.com/s/2AG04)。仅适用于 $(document).ready(function(){ 事件,这不想工作......

在客户端我有:

$(document).ready(function () {
    $.ajax({
        url: '@Url.Action("Index", "Product")',
        cache: false,
        type: 'GET',
        dataType: 'json',
        proccessData: false,
        contentType: 'application/json; charset=utf-8'
    });

在服务器上我有这个:

public ActionResult Index()
    {
        if (Request.IsAjaxRequest())
        {
            //Отправляем на клиент данные 
            _senderHub.SendMessage();
            return null;
        }
        return View();
    }

也在服务器上:(SignalR)

readonly ManagerDB _managerDB = new ManagerDB();
    public void SendMessage()
    {
        IEnumerable<ProductModels> list = _managerDB.GetListOfProduct1();
        var listToClient = new List<ProductModels>();
        foreach (var prod in list)
        {
            listToClient.Add(new ProductModels
                                 {
                                     Id = prod.Id,
                                     Name = prod.Name,
                                     LockType = prod.LockType,
                                     LockTime = prod.LockTime,
                                     LockUser = prod.LockUser,
                                     TimeStampF = prod.TimeStampF
                                 });
        }
        var anonimProduct = listToClient;
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<SenderHub>();
        context.Clients.AddListRows(anonimProduct);
    }

在客户端(SignalR)尝试捕获此数据:

$(function () {
    var senderHub = $.connection.senderHub;
    senderHub.AddListRows = function (data) {
        var dataFromServer = data;
        var listOfData = "";
        for (var i = 0; i < dataFromServer.length; i++) {
            $("#ListOfProductsTableBody").html(null);
            var userId = '';
            if (dataFromServer[i].LockUser != null) {
                userId = dataFromServer[i].LockUser;
            }
            listOfData += ("<tr><td>" + dataFromServer[i].Id + "</td><td>" + dataFromServer[i].Name + "</td><td>" + userId + "</td><td>" + dataFromServer[i].LockType + "</td>" + "<td id=\"ModifyBlock\"><a id=\"Detail\" href=\"#\" alt=" + dataFromServer[i].Id + " >Детально</a>|<a id=\"Delete\" href=\"#\" alt=" + dataFromServer[i].Id + " >Удалить</a>|<a id=\"Edit\" href=\"#\" class=\"" + dataFromServer[i].LockTime + "\" alt=" + dataFromServer[i].Id + " >Редактировать</a></td></td></tr>");
        }
        $("#ListOfProductsTableBody").append(listOfData);
    };
    $.connection.hub.start();
});

强调文字

1 个答案:

答案 0 :(得分:1)

请参阅David Fowler对我的问题的回答。看起来你有同样的问题。

Server to client messages not going through with SignalR in ASP.NET MVC 4