title: SignalR and MVC 5 partial views
body: <p>Hey guys I have the following issue with Asp SignalR. I want to have a chat with Admins and Users. I have 1 View with two divs (! for the Admin and 1 for the users), which I show or hide by checking the user. So far so good, everything works ! Both the admins and users get the necessary divs and can exchange messages - <a href="http://prnt.sc/dmzmen" rel="nofollow noreferrer">red div admin, blue div user.</a>. Take a look at the code and I'll explain where I get a problem.</p>
  
  <pre><code>&lt;div class="Admin" id="divMessageAdmin" style="background-color:red;"&gt;
      &lt;div class="welcome"&gt;&lt;/div&gt;&lt;br /&gt;
      &lt;div id="divWaitingUser"&gt;&lt;/div&gt;&lt;br /&gt;
      &lt;input id="txtMessage" type="text" /&gt;
      &lt;input id="btnSendMessage" type="button" value="Send" /&gt;
      &lt;div id="divAdminMessage"&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="User" id="divMessageUser" style="background-color:blue;"&gt;
      &lt;div class="welcome"&gt;&lt;/div&gt;&lt;br /&gt;
      &lt;input id="txtUserMessage" type="text" /&gt;
      &lt;input id="btnSendUserMessage" type="button" value="Send" /&gt;
      &lt;div id="divUserMessage"&gt;&lt;/div&gt;
  &lt;/div&gt;
  
      &lt;input id="hUserId" type="hidden" /&gt;
      &lt;input id="hId" type="hidden" /&gt;
      &lt;input id="hUserName" type="hidden" /&gt;
      &lt;input id="hGroup" type="hidden" /&gt;
  
  @section scripts {
      &lt;script src="~/Scripts/jquery-1.10.2.min.js"&gt;&lt;/script&gt;
      &lt;script src="~/Scripts/jquery.signalR-2.2.1.min.js"&gt;&lt;/script&gt;
      &lt;script src="~/signalr/hubs" type="text/javascript"&gt;&lt;/script&gt;
  
      &lt;script&gt;
          $(function () {
             var objHub = $.connection.chatHub;
             loadClientMethods(objHub);
             $.connection.hub.start().done(function () {
                 loadEvents(objHub);
             });
         });
  
         function loadEvents(objHub) {
  
             var name = '@HttpContext.Current.User.Identity.Name';
             objHub.server.connect(name);
             //alert(name);
  
             $('#btnSendMessage').click(function () {
                 var msg = $("#txtMessage").val();
                 if (msg.length &gt; 0) {
                     var username = $('#hUserName').val();
                     document.getElementById('txtMessage').value = "";
                     // &lt;&lt;&lt;&lt;&lt;-- ***** Return to Server [  SendMessageToGroup  ] *****
                     objHub.server.sendMessageToGroup(username, msg);
                 }
             });
  
             $('#btnSendUserMessage').click(function () {
                // alert("wrks");
                 var msg = $("#txtUserMessage").val();
                 if (msg.length &gt; 0) {
                     var username = $('#hUserName').val();
                     document.getElementById('txtUserMessage').value = "";
                     // &lt;&lt;&lt;&lt;&lt;-- ***** Return to Server [  SendMessageToGroup  ] *****
                     objHub.server.sendMessageToGroup(username, msg);
                 }
             });
  
             $("#txtMessage").keypress(function (e) {
                 if (e.which == 13) {
                     $('#btnSendMessage').click();
                 }
             });
         }
  
          function loadClientMethods(objHub) {
  
             objHub.client.getMessagesAdmin = function (userName, message) {
                     $(".txtMessage").val('');
                     $('#divAdminMessage').append('&lt;div&gt;&lt;p&gt;' + userName + ': ' + message + '&lt;/p&gt;&lt;/div&gt;');
                     var height = $('#divAdminMessage')[0].scrollHeight;
                     $('#divAdminMessage').scrollTop(height);
             }
  
             objHub.client.getMessagesUser = function (userName, message) {
                 $("#txtMessage").val('');
                 $('#divUserMessage').append('&lt;div&gt;&lt;p&gt;' + userName + ': ' + message + '&lt;/p&gt;&lt;/div&gt;');
                 var height = $('#divUserMessage')[0].scrollHeight;
                 $('#divUserMessage').scrollTop(height);
             }
  
             objHub.client.onConnected = function (id, userName, UserID, userGroup, flag) {
                 alert(flag);
                 var strWelcome = 'Welcome' + +userName;
                 $('.welcome').append('&lt;div&gt;&lt;p&gt;Welcome:' + userName + '&lt;/p&gt;&lt;/div&gt;');
                 $('#hId').val(id);
                 $('#hUserId').val(UserID);
                 $('#hUserName').val(userName);
                 $('#hGroup').val(userGroup);
                 if ( flag == "1") {
                     $("#divMessageUser").hide();
                     $("#divMessageAdmin").show();
                 }
                 else {
                     $("#divMessageUser").show();
                     $("#divMessageAdmin").hide();
                 }
             }
         }
      &lt;/script&gt;
  }
  </code></pre>
  
  <p>The thing is, that I want these two divs to be in separated Partial Views. This is what I'm trying. At the beginning of my page I check if the user is authenticated, if it is I fire a [ChildActionOnly] action method in my ChatController:</p>
  
  <pre><code>&lt;h2&gt;Chat&lt;/h2&gt;
  @{
      if (!User.Identity.IsAuthenticated)
      {
          @Html.Partial("_UnauthenticatedUserForm");
      }
      else
      {
          Html.RenderAction("AuthenticatedUsersChat");
      }
  }
  </code></pre>
  
  <p>and my action method in the controller</p>
  
  <pre><code>[ChildActionOnly]
  public ActionResult AuthenticatedUsersChat()
  {
      AppContext db = new AppContext();
      User user = db.Users.Single(usr =&gt; usr.Email == User.Identity.Name);
      int isAdmin = user.AdminCode;
      if (isAdmin == 0)
      {
          return PartialView("_UserChatPartial");
      }
      else
      {
          return PartialView("_AdminChatPartial");
      }
  }
  </code></pre>
  
  <p>this works and the partial views are returning in the way I want. In both partial views I've moved the divs ONLY! Admin Partial:</p>
  
  <pre><code>&lt;div class="Admin" id="divMessageAdmin" style="background-color:red;"&gt;
      &lt;div class="welcome"&gt;&lt;/div&gt;&lt;br /&gt;
      &lt;div id="divWaitingUser"&gt;&lt;/div&gt;&lt;br /&gt;
      &lt;input id="txtMessage" type="text" /&gt;
      &lt;input id="btnSendMessage" type="button" value="Send" /&gt;
      &lt;div id="divAdminMessage"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and UserPartial</p>
  
  <pre><code>&lt;div class="User" id="divMessageUser" style="background-color:blue;"&gt;
      &lt;div class="welcome"&gt;&lt;/div&gt;&lt;br /&gt;
      &lt;input id="txtUserMessage" type="text" /&gt;
      &lt;input id="btnSendUserMessage" type="button" value="Send" /&gt;
      &lt;div id="divUserMessage"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>BUT somehow then only the Administrator can see the messages. The user can send messages (admin receives it), but the user cannot see his or admins messages - <a href="http://prntscr.com/dmzvq3" rel="nofollow noreferrer">result</a>. I simply don't see any logic why only the user can't see the messages. Please if you have any ideas help me. Thanks in advance ! </p>
  
  <p>my method for the messages in the hub class </p>
  
  <pre><code>public void SendMessageToGroup(string userName, string message)
  {
      if (UsersList.Count != 0)
      {
          var strg = (from s in UsersList where (s.Email == userName) select s).First();
          MessageList.Add(new MessageInfo { UserName = userName, Message = message, UserGroup = strg.UserGroup });
          string strgroup = strg.UserGroup;
          Clients.Group(strgroup).getMessagesAdmin(userName, message);
          Clients.Group(strgroup).getMessagesUser(userName, message);
      }
  }
  // End SendMessage
  </code></pre>
  
  <p>and the connection method in the hub</p>
  
  <pre><code>public void Connect(string userName)
  {
          //if freeflag==0 ==&gt; Busy
          //if freeflag==1 ==&gt; Free
  
          //if tpflag==0 ==&gt; User
          //if tpflag==1 ==&gt; Admin
  
      var id = Context.ConnectionId;
      string userGroup = "";
  
      AppContext db = new AppContext();
      var userInfo = (from m in db.Users
                      where m.Email == HttpContext.Current.User.Identity.Name
                      select new { m.UserId, m.Email, m.AdminCode, m.FirstName, m.LastName }).FirstOrDefault();
  
      try
      {
          if ((int)userInfo.AdminCode == 0)
          {
              var strg = (from s in UsersList where (s.tpflag == "1") &amp;&amp; (s.freeflag == "1") select s).First();
              userGroup = strg.UserGroup;
              strg.freeflag = "0";
              //now add USER to UsersList
              UsersList.Add(new User { ConnectionId = id, UserId = userInfo.UserId, Email = userName, UserGroup = userGroup, freeflag = "0", tpflag = "0", });
              var flag = (from s in UsersList where (s.Email == userName) select s.tpflag); 
              Groups.Add(Context.ConnectionId, userGroup);
              Clients.Caller.onConnected(id, userName, userInfo.UserId, userGroup, flag);
          }
          else
          {
              UsersList.Add(new User { ConnectionId = id, UserId = userInfo.UserId, Email = userName, UserGroup = userInfo.AdminCode.ToString(), freeflag = "1", tpflag = "1" });
              var flag = (from s in UsersList where (s.Email == userName) select s.tpflag); 
              Groups.Add(Context.ConnectionId, userInfo.AdminCode.ToString());
              Clients.Caller.onConnected(id, userName, userInfo.UserId, userInfo.AdminCode.ToString(), flag);
          }
      }
      catch
      {
          Clients.Caller.NoExistAdmin();
      }
  }
  </code></pre>