【发布时间】:2018-04-11 13:24:58
【问题描述】:
我在 MVC(剃刀代码)的 Visual Studio 视图中。我在代码的不同区域多次使用相同的布尔“if statemant”。有人告诉我,我可以保存第一个 if 语句的结果并重复使用结果,这样我就不必多次编写“if 语句”了。
如何保存第一个 if 语句的结果并重复使用下面的结果?
var IsManager = someRole;
var IsTeamLead= someOtherRole;
if(IsManager || ITeamLead)
{
//Veiew this resticted content on the application;
}
//Code;
//Code;
if(IsManager || IsTeamLead)
{
//Veiew more resticted content on the application;
}
//Little more code;
//Little more Code;
if(IsManager || Is TeamLead)
{
//Veiew this content too;
}
//Little more code
//Little more code
//Little more code
if(IsManager || IsTeamLead)
{
//Veiew this content;
}
【问题讨论】:
-
我不确定他们给你的建议,但一种可能是this one
-
引入一个新变量,您可以像
bool isAuthorizedToView = someRole || someOtherRole;一样检查它现在您可以在任何地方使用if(isAuthorizedToView){...}。
标签: c# if-statement razor save code-reuse