【问题标题】:How to show user is online using Chatter Application MVC?如何使用 Chatter Application MVC 显示用户在线?
【发布时间】:2016-08-03 13:01:33
【问题描述】:

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

我有喋喋不休的控制器代码:

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>

我什么也得不到。我需要创建新的控制器?否则我可以覆盖现有的控制器?

【问题讨论】:

    标签: javascript c# asp.net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

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

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

    【讨论】:

    • 我仍然得到空白页@Shyju
    • 您的视图中有此代码吗?您是否看到任何消息(“没有用户”?)。你的意思是空白页?完整的空白页?什么都没有?然后,您的页面中可能还有其他错误。我建议你删除所有代码,然后把它放上去看看会发生什么
    • 我已经厌倦了删除所有代码把这个代码我得到空白页
    • 我需要在我的控制器中提及 HttpRuntime.Cache["ProjectId"] 吗?
    • 当然你需要有数据!如果没有数据,为什么要尝试查询。请参阅我回答的最后一句话。当您在缓存中没有该项目时,我还更新了答案以显示一条消息。试试看。
    猜你喜欢
    • 2011-12-13
    • 1970-01-01
    • 2020-05-06
    • 2014-05-30
    • 2022-01-13
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    相关资源
    最近更新 更多