【问题标题】:Ternary Operator in Multiple conditions多条件三元运算符
【发布时间】:2013-06-06 03:07:27
【问题描述】:

我在 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>
    }

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

使用显示和隐藏这个标签 (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>

但它不起作用。你能针对我的错误给出正确的解决方案吗?

【问题讨论】:

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


    【解决方案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)
    

    【讨论】:

    • 你的意思是说链接被隐藏了,但它仍然占用页面空间。我说的对吗?
    • 这一定是一个混乱。看到这个jsfiddle visibility: hidden 隐藏了元素,但它仍然占用布局中的空间。 display: none 从文档中完全删除元素。它不占用任何空间,即使它的 HTML 仍在源代码中。 - 根据this link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 2011-06-26
    • 2015-08-29
    • 2012-02-22
    • 2013-04-26
    • 2013-01-14
    • 2020-05-18
    相关资源
    最近更新 更多