如何通过SignalR传递复杂对象? [英] How to pass complex objects via SignalR?

查看:360
本文介绍了如何通过SignalR传递复杂对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个关于的优秀教程SignalR 解释了如何将.NET对象作为参数传递给Javascript,反之亦然。在这种情况下,它会传递一个 ChatMessage 对象。

There is an excellent tutorial on SignalR that explains how to pass .NET objects as parameters to Javascript and vice versa. In that case it passes a ChatMessage object to and from.

但是,本教程解决了一个非常简单的对象。我想看看如何在Javascript中处理复杂的.NET对象(其他对象作为属性)。

However, the tutorial addresses a really simple object. I'd like to see how to deal with complex .NET objects (that have other objects as properties) in Javascript.

例如,请考虑以下对象:

For instance, consider the following object:

class Master {
    public List<QuarterHour> QuarterHours { get; set; }
    public List<string> Books { get; set; }
    public int EndDay { get; set; }
    public int StartDay { get; set; }
}

class QuarterHour {
    public MinuteInstance Minute {get; set;}
    public int HourStart { get; set;}
}

class MinuteInstance { 
    public bool Registered {get; set;}
    public int NumAttendees {get; set;}
}

在.NET中,我可以引用这样的值: master.QuarterHours [2] .Minute.Registered 。我的问题:

In .NET, I can reference a value like this: master.QuarterHours[2].Minute.Registered. My questions:


  1. 我如何参考 master.QuarterHours [2] .Minute.Registered 在Javascript端的接收方法中?

  2. 如何在Javascript中构建 Master 对象以发送给.NET结束?

  1. How would I do reference master.QuarterHours[2].Minute.Registered in the receiver method on the Javascript end?
  2. How would I build the Master object in Javascript to be sent to the .NET end?


推荐答案


  1. 您只需发送和参考它是一样的。

  2. 你会通过(这是从服务器获取它时的样子):




{
    QuarterHours: [{
        Minute: {
            Registered: true,
            NumAttendees: 1337
        },
        HourStart: 1
    }],
    Books: ["Game of Thrones", "Harry Potter"],
    EndDay: 2,
    StartDay: 3
}


这篇关于如何通过SignalR传递复杂对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆