See my comments on the question for more context so that I don't repeat myself why I am posting this answer.
So we start by declaring a view model:
public class PlayersStatsViewModel { public int PlayersOnline { get; set; } public int TablesInPlay { get; set; } public int AvailableTables { get; set; } }
then we write a dedicated controller:
public class PlayersController: Controller { [ChildActionOnly] public ActionResult Index() { var model = new PlayersStatsViewModel(); // TODO: You should absolutely use DI here and replace this hardcoding using (var repository = new EFJugadorRepository()) { model.PlayersOnline = repository.FindAllJugadores().Where(j => j.jugEstado == 100).Count(); } // TODO: You should absolutely use DI here and replace this hardcoding using (var estadisticaMesaRepository = new EFEstadisticaMesaRepository()) { model.TablesInPlay = estadisticaMesaRepository.GetTablesInPlayCount; model.AvailableTables = estadisticaMesaRepository.GetAvailableTablesCount; } return PartialView(model); } }
next you write the corresponding partial view (~/Views/Players/Index.cshtml):
@model PlayersStatsViewModel <div id="players"> <p> Jogadores Online: <span class="onlinecount"> @Html.DisplayFor(x => x.PlayersOnline) </span> </p> <p> Mesas Jogando: <span class="onlinecount"> @Html.DisplayFor(x => x.TablesInPlay) </span> </p> <p> Mesas Disponiveis: <span class="onlinecount"> @Html.DisplayFor(x => x.AvailableTables) </span> </p> </div>
and the final step is to cleanup your layout:
<div id="header-content"> <a href="@Url.Action("Index", "Home")"> <img class="mainlogo" src="@Url.Content("~/Public/images/logo.png")"/> </a> @Html.Action("Index", "Players") <div id="login"> @Html.Partial("_LogOnPartial") </div> </div>
Don't ignore the two TODOs I left in the Index controller action. They are important. Right now your controller is tightly coupled to the way you are accessing your data (seems like EF or something). Layers in your application should be as tightly coupled as possible. And to achieve this you should work with abstractions. You should abstract away your data access layer behind interfaces. Then have your controller take those repository interfaces as constructor arguments. Finally you would simply configure your DI framework to serve the correct implementations (for example EF or whatever).
@Html.Action("FooAction", "Bar"). So you basically replace what you have shown in your view by a single call to the Html.Action helper. Would you like me posting an example or you will figure it out?