【问题标题】:Embed if statement inside a "a href" tag in Razor在 Razor 的“a href”标签内嵌入 if 语句
【发布时间】:2013-05-25 14:38:10
【问题描述】:

如何在“a href”标签中嵌入 if 语句。

例如

<a id="spnMarkButton" href="javascript:void(0);" @if(condition here) style="display:none;" else style="display:block;" onclick="MarkStore(@storeRating.StoreId);">

但上面的代码不起作用。

【问题讨论】:

    标签: html asp.net asp.net-mvc razor


    【解决方案1】:

    是否有特定原因需要将其嵌入标签中?

    @if(condition here)
    {
        <a id="spnMarkButton" href="javascript:void(0);" style="display:none;" onclick="MarkStore(@storeRating.StoreId);">
    }
    else
    {
        <a id="spnMarkButton" href="javascript:void(0);" style="display:block;" onclick="MarkStore(@storeRating.StoreId);">
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用 (?:) 操作符来做简单的事情,或者使用辅助类更好:

      @* Inline with MvcHtmlString *@
      <a id="spnMarkButton" href="javascript:void(0);" @(Model == null ? new MvcHtmlString("style=\"display:none;\"") : new MvcHtmlString("style=\"display:block;\""))>My link 1</a>
      
      @* Inline with Html.Raw *@
      <a id="spnMarkButton" href="javascript:void(0);" @(Model == null  ? Html.Raw("style=\"display:none;\"") : Html.Raw("style=\"display:block;\""))>My link 2</a>
      
      @* Using helper class - cleanest *@
      <a id="spnMarkButton" href="javascript:void(0);" @RenderDisplayStyle()>My link 3</a>
      
      @helper RenderDisplayStyle(){
          if (Model == null)
          {
              @:style="display:none"
          }
          else
          {
              @:style="display:block"
          }
      }
      

      在我看来,Helper 类是最干净的方式。

      【讨论】:

        【解决方案3】:

        你采取了错误的方法。

        在模型类中,像这样添加一个getter:

        string MarkButtonDisplay
        {
            get
            {
                if(condition here)
                    return "none";
                else
                    return "block";
            }
        }
        

        并将标记更改为:

        <a id="spnMarkButton" href="javascript:void(0);" style="display: @Model.MarkButtonDisplay;" onclick="MarkStore(@storeRating.StoreId);">
        

        不要混合逻辑和标记。

        【讨论】:

        • 不要混合逻辑和标记。这很有趣,因为这是非常真实的,因为它对于所有试图将接口与逻辑分离的模式都是乌托邦式的。看看您的示例:您正在将 CSS 值从标记移动到模型,这与您所说的完全相反。在我看来,这实际上是标记中的逻辑似乎完全合理的少数情况之一。我的 2 美分 :)
        • 如果我试图在public async Task OnGetAsync(string id)string id 中执行此操作是条件的一部分?
        【解决方案4】:

        试试这个:

        <a id="spnMarkButton" href="javascript:void(0);" @if(1==1) { <text>style="display:none;"</text> } else { <text>style="display:block;"</text> } onclick="MarkStore(@(storeRating.StoreId));">
        

        【讨论】:

          【解决方案5】:

          如果您使用代码块,这应该可以工作

          @if (condition) {…} else {…}
          

          【讨论】:

          • 它不起作用...
          • 您需要将您的风格包装在 标签中,即 style=""
          • 你在使用@if (condition) {style="display : none;"} else {…这是正确的剃须刀语法。
          猜你喜欢
          • 1970-01-01
          • 2016-06-04
          • 2017-12-28
          • 1970-01-01
          • 1970-01-01
          • 2017-03-24
          • 2012-03-25
          • 2012-11-28
          • 2021-03-03
          相关资源
          最近更新 更多