我正在创建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>
我什么也没得到。我需要创建新的控制器?或者我可以覆盖现有的控制器?
答案 0 :(得分:0)
您需要遍历字典项并显示它。
<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>
这将列出用户及其时间,假设HttpRuntime.Cache["ProjectId"]
存储字符串&amp;字典。在线用户的日期时间。
编辑: 显示如何将数据设置到缓存的示例代码。
这是你将项目添加到缓存的方法,你需要在你的应用程序的某个地方执行这样的代码(可能是当用户登录聊天,添加新条目时)。
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;