简体   繁体   English

使用C#方法的剃刀if语句

[英]Razor if-statement using c# method

I have a C# MVC project using Razor for views. 我有一个使用Razor进行视图的C#MVC项目。

In my _Layout.cshtml file i want to hide menuoptions based on bools in a helper class. 在我的_Layout.cshtml文件中,我想基于帮助器类中的布尔值隐藏菜单选项。

@using Project.HelpMethods;

<li id="menuOptionAppleIds">
@Html.ActionLink("A", "A", "Home")
</li>

How do i call a statis bool method in HelpMethods and use in as the expression in an if statement around the <li> element? 如何在HelpMethods中调用statis bool方法,并在<li>元素周围的if语句中将in用作表达式?

@if (Project.HelpMethods.IsOk())
{
    <li id="menuOptionAppleIds">
        @Html.ActionLink("A", "A", "Home")
    </li>
}
@if(Helper.ShowMenuOption(x))
{
 <li id="menuOptionAppleIds">
  @Html.ActionLink("A", "A", "Home")
 </li>
}

You can use if-statements and method calls in razor as in every other C# file. 您可以像其他所有C#文件一样在razor中使用if语句和方法调用。 The difference is to ad @symbol before the c# code 区别在于,在C#代码之前添加广告@symbol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM