thenna papertie thenna papertie - 4 months ago 22
ASP.NET (C#) Question

How to show user is online using Chatter Application MVC?

I am Creating MVC application chatter Application. I want show who is online in chatter. How can i show it?

I have chatter controller Code:

public ActionResult ChatterList(int ProjectId)
{
ReadCookieValue cookie = new ReadCookieValue();
clientId = cookie.readuserinfo().ClientId;
userId = cookie.readuserinfo().Id;
roleId = cookie.readuserinfo().RoleId;
ChatterViewModel model = new ChatterViewModel();
model.chattersList = Task.Run(() => _project.GetChatterList(ProjectId)).Result;
foreach (var item in model.chattersList)
{
item.CurrentUserId = userId;
}
model.newChatter = new ChatterModel();
model.newChatter.ProjectId = ProjectId;
model.newChatter.UserId = userId;
model.newChatter.UserName = cookie.readuserinfo().UserName;
return View("ProjectChatter", model);
}

public async Task<ActionResult> AddChatter(ChatterViewModel model)
{
if (model != null)
{
var obj = await _project.AddChatterList(model);
if (model.newChatter.ProjectId == 3)
{
return RedirectToAction("EbitDaReports");
}
}
return RedirectToAction("Reports");
}


and i created chatter using Project ID view Code looks like

<div class="col-lg-6">
<div class="ibox-content">
<div class="chat-discussion">

<div class="ibox float-e-margins">




<div class="chat-element right">
<div class="row m-t-lg">

@if (Model.ProjectId > 0)
{
@Html.Action("ChatterList", "Projects", new { @ProjectId = Model.ProjectId })
}
</div>

</div>



</div>
</div>


</div>
</div>


I am trying To use

<div class="row">
@if (HttpRuntime.Cache["ProjectId"] != null)
{
var loggedOnUsers = HttpRuntime.Cache["ProjectId"] as Dictionary<string, DateTime>;

if (loggedOnUsers != null)
{<div class="ProjectId">
}
<span> Online Users: </span>
</div>
}
}
</div>


I am getting Nothing. i need to create new controller ? or else i can override existing controller ?

Answer

You need to loop through the dictionary items and display that.

<div class="row">
    @if (HttpRuntime.Cache["ProjectId"] != null)
    {
        var loggedOnUsers = HttpRuntime.Cache["ProjectId"] as Dictionary<string, DateTime>;
        <span> Online Users: </span>
        if (loggedOnUsers != null)
        {
            foreach (var userItem in loggedOnUsers.Keys)
            {
                <p>@userItem <span>@loggedOnUsers[userItem]</span></p>
            }
        }
        else
        {
            <p>No users!</p>
        }

    }
    else
    {
      <h2>Could not find a HttpRuntime Cache entry with key "ProjectId"</h2>
    }
</div>

This will list the users and their time assuming that HttpRuntime.Cache["ProjectId"] stores a dictionary of string & date time for the online users.

Comments