2016-08-03 25 views
1

我正在创建MVC应用程序聊天应用程序。我想要显示谁在线聊天。我如何显示它?如何使用Chatter Application 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"); 
} 

,我使用Project 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

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

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

我还是收到一个空白页@Shyju –

+0

你有这段代码在你的视图中吗?你看到任何消息(“没有用户”?)。你的意思是空白页?完整的空白页面?没有什么?然后,您的网页中可能会出现其他一些错误。我建议你删除所有的代码,只是把这个,看看会发生什么 – Shyju

+0

我已经厌倦了删除所有的代码把这个代码我得到空白页 –