26

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?
AngryHacker
  • 59,598
  • 102
  • 325
  • 594

2 Answers2

23
  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
}
N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
  • I just changed my payload from to a (simple) complex type and it doesn't work anymore, the Send-Method in the hub doesn't get called like before. No error, nothing. Do you have any ideas? public async Task SendCommonMessage(CommonMessage commonMessage) – IngoB May 23 '21 at 00:10
9

You would want to serialize your class into a JSON object. There are many ways to accomplish this, but you can try JSON.NET to do it quick and easy.

If its not already included in your project, you can add this through Nuget with:

Install-Package Newtonsoft.Json 

The code would look something like:

var json = JsonConvert.SerializeObject(master);

Once this is passed to your client-side, you can then read from your JSON object like any other. You can use the following javascript code to convert your SignalR string message to a JSON object:

var master = JSON.stringify(eval("(" + message + ")"));
var registered = master.QuarterHours[2].Minute.Registered;

You can also pass this through SignalR to the server and deserialize the JSON object using JsonConvert.DeserializeObject in order to convert it to your C# classes. Check out the documentation here for further details: http://james.newtonking.com/projects/json/help/

SeanPrice
  • 464
  • 2
  • 8
  • 1
    the question was on how to pass complex objects to singalR, not how to serialize objects. – Sonic Soul May 15 '14 at 18:51
  • 22
    ...and one solution is by serializing objects into JSON. – Danny Bullis Apr 30 '15 at 19:48
  • 5
    If there is a native way to send complex objects (as explained in the other answer), using serialization is not really a "good" solution, but a workaround - especially because it seems that serialization is what SignalR does internally.. so basically you are rewriting part of the library. – marsop Jan 22 '18 at 07:54
  • I just used SignalR to send a .NET object. The client-side js receives a js object in the same structure. I didn't have to serialize it at all. Make sure the properties are public! – Xpleria Oct 24 '18 at 14:40
  • 2
    you do not need manually serialize objects in signalR. its already implemented in signalr lib – AliReza Sabouri Oct 30 '18 at 11:56