簡體   English   中英

c#如果控件在單行中

[英]c# If Control in single row

我正在使用asp.net mvc視圖,以便使用if控件。

我的代碼:

<div class="panel-body @(((ObjectModelLibrary.Law)ViewData["currentLaw"]).LastStatus == "" ? "collapse" : string.Empty)">

關於上述if控制的我的問題:

我要檢查

If ((ObjectModelLibrary.Law)ViewData["currentLaw"]).LastStatus == null ||
If ((ObjectModelLibrary.Law)ViewData["currentLaw"]).LastStatus == ""

我要用? "collapse" : else : string.Empty)"> ? "collapse" : else : string.Empty)">

如何在代碼的一行中使用If not null和if not“”?

如果要檢查字符串值是null還是""則只需使用IsNullOrEmpty

string.IsNullOrEmpty(((ObjectModelLibrary.Law)ViewData["currentLaw"]).LastStatus) ? "collapse" : string.Empty

如果要在條件表達式中組合多個條件,則可以使用邏輯運算符進行組合。 例如:

if (someCondition && someOtherCondition)

這將要求兩個條件都成立。 (如果改用||運算符,則它將要求至少一個條件為true。)

因此,在?:運算符中,可能類似於:

(someCondition || someOtherCondition) ? someValue : someOtherValue

條件表達式的邏輯段可以用括號分組,因此您可以根據需要添加任意數量:

(condition1 || (condition2 && condition3)) ? someValue : someOtherValue

從技術上講,您可以構造任意大小的條件和其他表達式,盡管當然,代碼的可讀性和可維護性很快成為問題。 將邏輯提取到單獨的方法中有助於使代碼更加井井有條。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM