You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Syncfusion ASP.NET Core数据绑定后获取cshtml中的count值?

获取Syncfusion Grid的总记录数(客户端)

针对你在ASP.NET Core 7.0中使用Syncfusion Grid的场景,后端返回的count参数可以通过以下两种客户端方式获取:

方法1:通过dataBound事件获取

每次Grid完成数据绑定后,会触发dataBound事件,事件参数中直接包含总记录数。

步骤1:修改Grid配置添加事件

在cshtml的Grid标记中添加dataBound事件绑定:

<ejs-grid id="userstbl" allowSorting="true" allowPaging="true"
          toolbarClick="toolbarClick" dataBound="onDataBound"
          toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
    <e-grid-pagesettings pageCount="6" pageSize="50"></e-grid-pagesettings>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal"></e-grid-editSettings>
    <e-data-manager url="/Users/FetchData" adaptor="UrlAdaptor"></e-data-manager>
    <e-grid-columns>
        <e-grid-column headerText="First Name" field="FirstName"></e-grid-column>
        <e-grid-column headerText="Family" field="Family"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
<!-- 可选:用于显示总记录数的页面元素 -->
<div id="totalCountDisplay"></div>

步骤2:编写事件处理函数

在页面的<script>标签中添加事件逻辑:

function onDataBound(args) {
    // 从事件参数中提取总记录数
    const totalCount = args.totalRecordsCount;
    // 打印到控制台查看
    console.log("总记录数:", totalCount);
    // 将数值显示到页面元素中
    document.getElementById("totalCountDisplay").textContent = `总记录数:${totalCount}`;
}

方法2:通过Grid实例主动获取

如果需要在特定时机(比如按钮点击、自定义操作)获取总记录数,可以通过Grid实例的getTotalRecordsCount()方法直接获取:

// 获取Grid实例
const gridInstance = document.getElementById("userstbl").ej2_instances[0];
// 调用方法获取总记录数
const totalCount = gridInstance.getTotalRecordsCount();
console.log("总记录数:", totalCount);

说明

Syncfusion的DataManager会自动解析后端返回的count字段,并将其维护在Grid内部状态中,无需手动解析AJAX返回的JSON数据,直接通过上述方式即可拿到总记录数。

内容的提问来源于stack exchange,提问作者topcool

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 20:53:12