<div id="header-content">
<a href="@Url.Action("Index", "Home")">
<img class="mainlogo" src="@Url.Content("~/Public/images/logo.png")"/>
</a>
@{
using (EFJugadorRepository repository = new EFJugadorRepository())
{
var playersOnline = repository.FindAllJugadores().Where(j => j.jugEstado == 100).Count();
}
using (EFEstadisticaMesaRepository estadisticaMesaRepository = new EFEstadisticaMesaRepository())
{
var tablesInPlay = estadisticaMesaRepository.GetTablesInPlayCount;
var availableTables = estadisticaMesaRepository.GetAvailableTablesCount;
}
}
<div id="players">
<p>Jogadores Online: <span class="onlinecount">@playersOnline</span></p>
<p>Mesas Jogando: <span class="onlinecount">213</span></p>
<p>Mesas Disponiveis: <span class="onlinecount">850</span></p>
</div>
<div id="login">
@Html.Partial("_LogOnPartial")
</div>
</div>
我正在尝试在我的Razor代码中使用@playersOnline
变量,但它表示它未被声明。
无法解析符号'playersOnline'。
知道为什么我无法访问该变量吗?
是因为它是在using
声明中声明的吗?
答案 0 :(得分:4)
是的,这是因为它在using
声明中声明。
using语句中声明的所有内容都是针对该代码块的作用域。与if
语句相同。
只需将变量decleration放在using:
之外int playersOnline = 0;
using (EFJugadorRepository repository = new EFJugadorRepository())
{
playersOnline = repository.[...]).Count();
}
正如评论中指出的那样,您的代码似乎不属于视图层。考虑一下如何构建代码并从Darin那里获取一些技巧。
答案 1 :(得分:1)
是因为它是在using语句中声明的吗?
是。此变量在using
范围之外不可见。
您可以这样做:
<div id="players">
@using (EFJugadorRepository repository = new EFJugadorRepository())
{
var playersOnline = repository.FindAllJugadores().Where(j => j.jugEstado == 100).Count();
<p>Jogadores Online: <span class="onlinecount">@playersOnline</span></p>
}
<p>Mesas Jogando: <span class="onlinecount">213</span></p>
<p>Mesas Disponiveis: <span class="onlinecount">850</span></p>
</div>
答案 2 :(得分:1)
有关更多背景信息,请参阅我对该问题的评论,以便我不再重复自己为何发布此答案。
所以我们首先声明一个视图模型:
public class PlayersStatsViewModel
{
public int PlayersOnline { get; set; }
public int TablesInPlay { get; set; }
public int AvailableTables { get; set; }
}
然后我们写一个专用的控制器:
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);
}
}
接下来你写相应的局部视图(~/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>
最后一步是清理你的布局:
<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>
不要忽略我在索引控制器操作中留下的两个TODO。它们很重要。现在,您的控制器与您访问数据的方式紧密相关(看起来像EF或其他东西)。应用程序中的图层应尽可能紧密耦合。要实现这一点,你应该使用抽象。您应该在接口后面抽象出数据访问层。然后让您的控制器将这些存储库接口作为构造函数参数。最后,您只需配置DI框架以提供正确的实现(例如EF或其他)。