【问题标题】:Making an AJAX callback for every row in a table / MVC 4 ASP.NET为表/MVC 4 ASP.NET 中的每一行进行 AJAX 回调
【发布时间】:2023-04-06 08:21:01
【问题描述】:

我正在创建一个视图,它将在收到响应后 ping 服务器列表并显示状态:

  • 服务器 1 - * 等待响应 *
  • 服务器 2 - * 收到响应 *
  • 服务器 3 - * 等待响应 *

在页面加载时,它们都会(理论上)显示 * Waiting for response *,但是当收到每个响应时,表格将被更新。响应可能需要 0 到 30 秒之间的任何时间。

我需要为每一行设置一个 ajax 回调。

我是一名 Windows 开发人员,因此在谈到 web/ajax 时,我一直在努力寻找最佳实践。解决这个问题的正确方法是什么?

【问题讨论】:

  • 谁立即否决了这个问题 - 我会很感激一个理由。我错过了什么吗?

标签: jquery jquery-ui asp.net-mvc-4 razor asp.net-ajax


【解决方案1】:

我觉得jquery ajax 函数就够了。要更新网站,您可以使用 Knockout/AngularJS。

您可以创建 KO 可观察字符串数组并执行以下操作:

 rows = ko.observableArray();

// this in onload

//fill rows with { ServerName, Status = "Waiting for response"}

 $.ajax({
            type: "GET",
            url: "yoururl/function/row" //row is row number
        }).done(function(){ rows()[row].Status = "Response received"; });

和html:

<table>
<!-- header here -->
<tbody data-bind="foreach: rows">
                    <tr>
                        <td data-bind="text: ServerName"></td>
                        <td data-bind="text: Status"></td>
                    </tr>
</tbody>
</table>

【讨论】:

  • 干杯,我会调查一下。
猜你喜欢
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多