1

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

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

4

1 回答 1

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)
于 2013-06-10T12:55:15.090 回答