Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass complex objects via SignalR?

Tags:

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.

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;} } 

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

  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?
like image 838
AngryHacker Avatar asked Jun 27 '13 18:06

AngryHacker


1 Answers

  1. You just send it and reference it the same way.
  2. You'd pass (this is how it looks when you get it from the server):
{     QuarterHours: [{         Minute: {             Registered: true,             NumAttendees: 1337         },         HourStart: 1     }],     Books: ["Game of Thrones", "Harry Potter"],     EndDay: 2,     StartDay: 3 } 
like image 182
N. Taylor Mullen Avatar answered Sep 20 '22 03:09

N. Taylor Mullen