@if 与多个 OR 和 if 内部的最佳语法

我需要你关于 C# 语法的帮助:


这是我的实际代码:


<div class="class">

    <h5>MySuperTitle</h5>

    <ul>

        @if (Ms.Cs.value1 != null)

        {

            <li> My Li1 <span>@Ms.Cs.value1</span> </li>

        }

        @if (Ms.Cs.value2 != 0)

        {

            <li> My Li2 <span>@Ms.Cs.value2</span> </li>

        }

        @if (true)

        {

            <li> My Li3 <span>--</span></li>

        }

        @if (Ms.Cs.value4 != 0)

        {

            <li> My Li4 <span>@Ms.Cs.value4</span></li>

        }

        @if (Ms.Cs.value5 != null)

        {

            <li> My Li5 <span>@Ms.Cs.value5</span></li>

        }

    </ul>

</div>

我正在寻找的是在“MySuperTitle”上添加一个@if条件以在没有“sub”值时隐藏它。


我必须添加一个@if (Ms.Cs.value1 != null) || (Ms.Cs.value2 != 0) || (Ms.Cs.value4 != 0) .....吗?


或者有更好的语法吗?非常感谢


MMTTMM
浏览 241回答 2
2回答

慕姐4208626

我假设您正在使用 ViewModel。如果是这样,在您的 ViewModel 中,您可能希望这样做:public bool Value1Exists{&nbsp; &nbsp; get&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return Value1 != null&nbsp; &nbsp; }}...public bool HasItems{&nbsp; &nbsp; get&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Value1Exists || Value2Exists || ...)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return true;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}然后在您的剃刀视图中:@if(Ms.HasItems) {&nbsp; &nbsp; <h5>MySuperTitle</h5>&nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; @if (Ms.Value1Exists) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; </ul>}
打开App,查看更多内容
随时随地看视频慕课网APP