show the list of usernames inside a div

*爱你&永不变心* 提交于 2020-01-14 19:07:41

问题


I am new to jQuery. In my project, I created one Class User in which the code is as shown below:

 static ConcurrentDictionary<string, User> _users = new ConcurrentDictionary<string, User>();
 //
 //  some function to add values to _users list
 //
 public List<User> GetConnectedUsers()
 {
      return _users.Values.ToList();
 }

I want to show this list of values in div #showUsernames. How to do this by using jQuery?

I tried the below which is not showing anything

    /*display your contacts*/
    $('#showUsernames').append(function(){
        chat.server.getConnectedUsers();
    });

where chat.server(signalr) calls the server-side code(Chat class)..
I think the solution has nothing to do with signalr.

If the above description is not enough then you can go through my code here

EDIT: User.cs

public class User
{
    public User(string ConnID, string Username)
    {
        Name = Username;
        ConnectionID = ConnID;
    }
    public string Name { get; set; }
    public string ConnectionID { get; set; }
}

回答1:


Calls to the server are async (like all javascript ajax calls). Attach a .done handler to get the results (this is all in the documentation https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs).

Change your code to be like this:

chat.server.getConnectedUsers().done(function(users) {
    for(var i = 0; i < users.length; ++i) {
        // Write javascript here to append users to the list
    }
});


来源:https://stackoverflow.com/questions/13776648/show-the-list-of-usernames-inside-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!