javascript - 将数据从 JavaScript 传递到 C#

标签 javascript c# asp.net

所有操作都在 View 中。

我的应用程序现在的样子:我从 websocket 服务器获取 JSON 数据并将其保存在某个 JS 变量中。

我想在我的 View 中创建此数据的动态内容表。我的想法是以某种方式(?)从 js 传递数据到 asp.net View ,并使用 for 循环创建该数据的表。

我应该尝试使用ajax吗?以及如何传递这些数据?

最佳答案

如果您只想从收到的 json 数据创建 TableView ,您可以使用 DataTables 插件,它是一个非常流行的 jQuery Javascript 库和高度灵活的工具,您将轻松获得许多附加功能,如排序、过滤。

这是一个您可以遵循的示例。

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "scripts/jsonp.php",
            "dataType": "jsonp"
        }
    } );
} );
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
 
        <tfoot>
            <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>

您将从源站获得更多有用的示例:DataTable

此外,如果您使用 AngularJS 或 KnockoutJS 等 JavaScript 框架,您还可以通过许多其他方式执行此操作。

关于javascript - 将数据从 JavaScript 传递到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33519102/

相关文章:

javascript - 如何在不使用 jQuery 的情况下添加点击处理程序

c# - ActionFilter 返回带有消息的 Http 状态代码

c# - 试图让 Windows Phone 8 ListPicker 工作

c# - 更改资源文件后出现歧义错误

javascript - 如何检测 SVG 中的鼠标悬停矩形

javascript - 如何使用 DataTables.net 解决破坏容器外部表的长列值问题?

javascript - 在小老鼠上执行 javascript 函数文本内容

c# - WebBrowser - 查找呈现文本的高度

asp.net - 如何执行 SQL 语句?

c# - 使用process.waitforexit()时线程被中止错误