【发布时间】:2015-06-14 06:38:50
【问题描述】:
为了制作实时的 .NET Web 应用程序,我使用 SignalR for Kendo 网格,它与网格上的读取、更新、销毁方法一起使用。
但是,我的情况是从其他页面创建和更新记录,剑道网格仅用于读取数据。我想实现 SignalR 以在将新记录添加到数据库时通知用户。
这是我的代码
SignalRHub class 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();
}
}
索引.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
});
【问题讨论】:
-
感谢 Anik,我在实现剑道网格时已经阅读了这篇文章。在文章中,示例显示 SignalR 仅在您在网格中执行 CRUD 时才有效。我的问题是,如果您在网格之外(例如在其他页面上)执行 CUD 并且只在 Kendo Grid 中使用 Read,我们该怎么做?
-
@JustinPham 你找到解决方案了吗?如果是这样,您能否更新/回答您的问题?
标签: c# asp.net-mvc kendo-ui push-notification signalr