[英]Can't access a variable declared inline in Razor code
<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
语句中声明的吗?
是的,因为它是在using
语句中声明的。
using语句中声明的所有内容都适用于该代码块。 与if
语句相同。
只需在使用之外放置变量清零:
int playersOnline = 0;
using (EFJugadorRepository repository = new EFJugadorRepository())
{
playersOnline = repository.[...]).Count();
}
正如注释中指出的那样,您的代码似乎不属于视图层。 考虑一下如何构造代码,并从Darin中获取一些技巧。
是因为它是在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>
有关更多背景信息,请参见我对该问题的评论,以便我不再重复我为什么要发布此答案。
因此,我们首先声明一个视图模型:
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或其他功能)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.