【问题标题】:how to create an else if statement in Razor?如何在 Razor 中创建 else if 语句?
【发布时间】:2011-11-19 22:00:50
【问题描述】:

我正在尝试在表格中显示一些行。根据用户组,视图应该显示不同的标记。管理员可以删除行,但版主只能将它们标记为可见或不可见。

如何在 Razor 中编写正确的 if else 语句?

页面显示正确,但是页面标题是Parse Error

这是我的代码:

@model MvcApplication3.Models.ViewModels.New.Question.MatrixRows

@{
    bool visible = Model.Visible;
}

<tr>
    <td>
    @if(visible) 
        {
        @Html.TextBoxFor(cn => Model.Row_Number, new { @class = "row required digits", size = 1 })
        }
    @if (!visible)
        {
        @Html.TextBoxFor(cn => Model.Row_Number, new { @class = "row required digits", size = 1, disabled = "disabled" })
        }
    </td>
    <td>
    @if(visible) 
        {
            @Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45 })
        }
    @if (!visible)
    {
        @Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45, disabled = "disabled" })
    }
    </td>
    <td>
        @if (HttpContext.Current.User.IsInRole("Administrator"))
        {
            @Html.HiddenFor(x => x.Delete, new { @class = "mark-for-delete" })
            @Html.LinkToRemoveNestedForm("Slet", "tr", "input.mark-for-delete")    
        }
        @if (HttpContext.Current.User.IsInRole("Moderator"))
        {
            @Html.HiddenFor(x => x.Visible, new { @class = "mark-for-visible" })
            @Html.LinkToDisableNestedForm("Deaktiver", "tr", "input.mark-for-visible")    
        }
        @Html.HiddenFor(id => Model.Row_Id)
    </td>
</tr>

【问题讨论】:

  • 既然你说页面显示正确,问题是视图内的逻辑还是页面的title
  • 逻辑一定是错误的,因为它给出了错误标题?

标签: asp.net-mvc-3 razor


【解决方案1】:

标题有解析错误,因为你没有设置标题:

@{
    ViewBag.Title = "Home Page";
}

现在对于 else 语句,不要使用 @ 语法:

@if(visible) 
{
    Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45 })
}
else
{
    Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45, disabled = "disabled" })
}

你正在检查一个布尔值,你只需要一个 else。对于else if,也是一样的。

您的代码可以通过以下方式进一步简化:

@Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45, disabled = visible ? "" : "disabled" })

因为无论如何您都在显示相同的代码,只是根据值更改属性。对我来说,这变得更具可读性。

【讨论】:

    【解决方案2】:

    因为问题是如果在剃须刀中,我将其用于 SEO 目的

    可见

    @{
        if (ViewBag.Option == "Mobiles")
    {
        ViewBag.Title = "Mobiles";
        ViewBag.Description = "Mobiles";
        ViewBag.Keywords = "Mobiles";
    
    }
    
       else if (ViewBag.Option == "holiday")
    {
        ViewBag.Title = "holiday";
        ViewBag.Description = "holiday.";
        ViewBag.Keywords = "holiday";
    }
    
    
    else
        {
            ViewBag.Title = "home";
            ViewBag.Description = "home";
            ViewBag.Keywords = "home";
        }
    
    }
    

    希望能帮助别人。

    【讨论】:

      【解决方案3】:

      您只需使用else 而不在前面加上@。但是,我认为这不是您的页面标题的问题。也许您需要设置ViewBag.Title 属性?您的布局页面可能取决于它的设置。

      @if(visible) 
          {
          @Html.TextBoxFor(cn => Model.Row_Number, new { @class = "row required digits", size = 1 })
          }
      else
          {
          @Html.TextBoxFor(cn => Model.Row_Number, new { @class = "row required digits", size = 1, disabled = "disabled" })
          }
      

      标题问题:

      @{
          bool visible = Model.Visible;
          ViewBag.Title = "My Title;
      }
      

      【讨论】:

      • 好的,我更正了它,但我仍然收到 Parse Error 标题。怎么办?
      • 您更正了ViewBag.Title?在您看到我的原始答案后,我可能已经添加了这一点。
      猜你喜欢
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多