gpt4 book ai didi

javascript - 如何使用 Chatter 应用程序 MVC 显示用户在线?

转载 作者:行者123 更新时间:2023-11-28 05:43:21 25 4
gpt4 key购买 nike

我正在创建 MVC 应用程序 chatter 应用程序。我想在聊天中显示谁在线。我怎样才能显示它?

我有chatter Controller 代码:

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 View 创建了聊天代码看起来像

<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>

我什么也没得到。我需要创建新的 Controller ?或者我可以覆盖现有的 Controller ?

最佳答案

您需要循环遍历字典项目并显示它。

<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"] 存储在线用户的字符串和日期时间字典。

编辑: 显示示例代码如何将数据设置到缓存。

这就是将项目添加到缓存的方式,您需要在应用程序中的某个位置执行这样的代码(可能是当用户登录聊天时,添加新条目)。

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;

关于javascript - 如何使用 Chatter 应用程序 MVC 显示用户在线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38744444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com