I am trying to return the results of a search by using SignalR. I would rather not use AJAX in this case as it will mean I have to build further controller methods etc, and as my application is largely built on SignalR I would prefer to use it exclusively.
My Hub Method returns some contacts, serialised as Json:
public string SearchContacts(string search) {
return _serializer.Serialize(_db.Contacts_SearchContacts(search.Trim()).Select(o => new Contact(o.FullName,o.ContactId)).ToList());
}
My client method calls this method and deserialises the result:
function findMatches (q,contactsHub) {
findMatches(q) {
var matches = contactsHub.server.searchContacts(q);
return JSON.parse(matches);
};
}
The method call works ok, the server method runs, the client gets back results but the result string always comes back as "[object Object]". I have checked the result on the server, and the serialised string is correct, so somehow it is getting lost/garbled on route back to the client.
What am I missing?
Figured this out. It can be done simply by working with the "done" function:
function findMatches (q,contactsHub) {
findMatches(q) {
contactsHub.server.searchContacts(q).done(function(result) {
return JSON.parse(result);
});
};
}