多个条件下的三元算子

时间:2013-06-10 12:47:43

标签: c# asp.net asp.net-mvc-4 ternary-operator logical-operators

我在c#

中有一个if条件
    if (item.ReporSubCategoryId == 1 || item.ReporSubCategoryId == 2 || 
        item.ReporSubCategoryId == 3 || item.ReporSubCategoryId == 4)
    {
<a  href="@Url.Action("GetPdf", "Report", new { report = item.ReporSubCategoryId })" >@item.ReportTitle</a>
    }

如果条件改变使用三元运算符,我想这样做吗?

使用show并隐藏这个标签 (style='display:Myconditions?block:None'

<a  href="@Url.Action("GetPdf", "Report", new { report = item.ReporSubCategoryId })" >@item.ReportTitle</a>

但是我试试这段代码

<a style='display:" @item.ReporSubCategoryId == 1 || @item.ReporSubCategoryId == 2 || @item.ReporSubCategoryId == 3 || @item.ReporSubCategoryId == 4 ?block:None"'href="@Url.Action("GetPdf", "Report", new { report = item.ReporSubCategoryId })" >@item.ReportTitle</a>

但它不起作用。你能为我的错误提供正确的解决方案吗?

1 个答案:

答案 0 :(得分:1)

试试这个..

@Html.ActionLink(item.ReportTitle, "GetPdf", "Report", new { report = item.ReporSubCategoryId }, 
 new { @style = "display:" 
  (item.ReporSubCategoryId == 1 || item.ReporSubCategoryId == 2 || item.ReporSubCategoryId == 3 || item.ReporSubCategoryId == 4)
   ? "block" : "none" }
})

根据您的情况添加“阻止”/“无”

(item.ReporSubCategoryId == 1 || item.ReporSubCategoryId == 2 || item.ReporSubCategoryId == 3 || item.ReporSubCategoryId == 4)