如何使用Chatter应用程序MVC显示用户在线
How to show user is online using Chatter Application MVC?
我正在创建MVC应用程序聊天应用程序。我想展示谁在网上聊天。我该怎么展示?
我有聊天控制器代码:
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");
}
我用项目ID视图创建了聊天代码看起来像
<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>
我正在尝试使用
<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>
我一无所获。我需要创建新的控制器?或者我可以覆盖现有的控制器?
您需要循环遍历字典项并显示它。
<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>
这将列出用户和他们的时间,假设CCD_;在线用户的日期时间。
EDIT:显示如何将数据设置到缓存的示例代码
这就是你将项目添加到缓存的方式,你需要在应用程序中的某个地方执行这样的代码(可能是当用户登录聊天时,添加一个新条目)。
var data = HttpRuntime.Cache["ProjectId"] as Dictionary<string, DateTime>;
if(data==null)
data=new Dictionary<string, DateTime>();
data.Add("Scott",DateTime.Now);
data.Add("Shyju", DateTime.Now);
data.Add("Kevin", DateTime.Now);
HttpRuntime.Cache["ProjectId"] = data;
相关文章:
- 如何在页面一侧的浮动框中显示用户脚本的输出
- 如何在同一页面上显示用户提交的数据
- 在谷歌地图上显示用户当前位置并更新
- 谷歌标记不显示用户定义的点
- 显示用户's在图像地图上的位置
- Javascript-使用Facebook登录,显示用户's成功登录后的个人资料图片
- 如何在地图上显示用户位置以及我的标记
- 如何使用“地图”在谷歌地图上显示用户位置;onclick”;作用
- 使用PHP显示用户输入
- 在文本区域中的文本中显示用户选择的文本
- 显示用户选择的文本
- 打开 d3 树布局,通过在搜索框中键入 request 来显示用户搜索的节点
- 如何显示用户在标签中写入的输入文本
- 显示用户选择中的某些表行
- Javascript 显示用户输入
- 如何显示用户使用文本区域的日期和时间(单击时与本地存储一起保存)
- 在提交表单之前显示用户端错误
- 显示用户选择
- Javascript函数显示用户输入数组
- 隐藏/显示用户列组的菜单项