c# - 如何将 SignalR 数据源与 Kendo 网格一起使用

标签 c# asp.net-mvc kendo-ui push-notification signalr

为了制作一个实时的 .NET Web 应用程序,我将 SignalR 用于 Kendo 网格,它与网格上的读取、更新、销毁方法一起工作。

但是,我的情况是从其他页面创建和更新记录,Kendo Grid 仅用于读取数据。我想实现 SignalR,以便在将新记录添加到数据库时通知用户。

这是我的代码。

SignalRHub 类:

public class SignalRHub : Hub
{
    private DbEntities db;
    
    public SignalRHub()
    {
        db = new DbEntities();
    }

    public IEnumerable<ViewTicketModel> Read()
    {
        return db.Post_User.AsEnumerable()
            .Select(ticket => new ViewTicketModel
            {
                Id = ticket.Id,
                BuyerName = ticket.Name,
                DateCreated = ticket.CreatedOn.ToString("dd/MM/yyyy"),
                BuyerPhoneNumber = ticket.Mobile,
                Details = ticket.Details,
                Location = ticket.Location,
            })
            .OrderByDescending(x => DateTime.ParseExact(x.DateCreated, "dd/MM/yyyy", null))
            .ToList();
    }
}

Index.cshtml:

var connection = $.connection;
var hub = connection.signalRHub;
var hubStart = connection.hub.start();

console.log("here");

var signalRDataSource = new kendo.data.DataSource({
    type: "signalr",
    autoSync: true,
    push: function(e) {
        var notification = $("#notification").data("kendoNotification");
        notification.success(e.type);
    },
    schema: {
        model: {
            id: "Id",
            fields: {
                "Id": { editable: false, type: "Number" },
                "BuyerName": { type: "string" },
                "DateCreated": { type: "string" },
                "BuyerPhone": { type: "string" },
                "Details": { type: "string" },
                "Location": { type: "string" }
            }
        }
    },
    transport: {
        signalr: {
            promise: hubStart,
            hub: hub,
            server: {
                read: "read",
            },
            client: {
                read: "read",
            }
        }
    },
    pageSize: 10,
});

$("#grid").kendoGrid({
    height: 700,           
    filterable: {
        extra: false,
    },
    pageable: true,
    sortable: {
        mode: "multiple",
        allowUnsort: true
    },
    columns: [
        { field: "Id", title: "Notification Id", width: 100, hidden: true },
        {
            field: "DateCreated",
            title: "Date Created",
            width: 150,
            filterable: {
                ui: "datetimepicker"
            }
        },
        { field: "Location", title: "Location", width: 150 },                   
        { field: "BuyerName", title: "Buyer Name", width: 120, hidden: true },
        { field: "BuyerPhoneNumber", title: "Buyer Phone", width: 120, hidden: true },
    ],
    dataSource: signalRDataSource
});

最佳答案

其他页面,你的意思是不同的应用程序?如果是这样的话,这将使问题复杂化。

记住剑道网格只有 4 个默认信号操作(创建、更新、读取、销毁)。任何其他的都必须由您实现。

这是一个示例,说明如何让“已连接”的客户端进行刷新读取。

在你的中心:

IHubContext context = GlobalHost.ConnectionManager.GetHubContext<Dashboard>();
        context.Clients.All.reload();

在您的客户端 html 页面中:

<script type="text/javascript">
var hub = $.connection.dashboard;
hub.client.reload = function () {
    updategrid();
};
var hubStart = $.connection.hub.start();
function updategrid()
{
    $('#GridName').data('kendoGrid').dataSource.read();
    $('#GridName').data('kendoGrid').refresh();
}</script>

这将强制所有连接的客户端进行刷新。 完美的场景是将适当的更改推送给客户端。这个实现可能会变得棘手,因为您不知道用户有什么过滤器/排序/分组。然而,这是可以实现的。您可以让每个连接的客户端将其过滤器/分组/排序发送回服务器并仅提取适当的更改。

关于c# - 如何将 SignalR 数据源与 Kendo 网格一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29529318/

相关文章:

jquery - 复选框选择上的文本删除线

c# - 使用 ImpromptuInterface 调用基类的私有(private)成员

C#人脸识别

c# - 如何在 .net 中将 int 转换为 decimal

asp.net-mvc - 如何更改 MVC Core ValidationSummary 的默认错误消息?

c# - 数据注释上的正则表达式在子属性上的行为不同

html - 如何在 kendo ui 中以编程方式从 treeview 的远程 json 数据更改选定的节点颜色?

javascript - Typescript Kendo UI 网格列类型错误

jquery - 如何更改已过滤的单元格中的过滤器颜色

c# - 底层连接已关闭 : - WebClient error