2015-03-31 64 views
-1

只是想,如果加上内嵌在我的部分观点,但不断收到预期}内嵌如果预期}

每5项我想在自己的表

@model IList<PAL.Intranet.Models.MonitorDetailModel> 

<div> 
    @for (int i = 0; i < @Model.Count(); i++) 
    { 
     <table> 
      <tr> 
       @if (i % 5 == 0) 
       { 
        <tr> 
       } 

       <td><img src="@if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.OK) 
           { @PAL.Intranet.Models.AlertStateImage.OK } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Alert) 
           { @PAL.Intranet.Models.AlertStateImage.Alert } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Warning) 
           { @PAL.Intranet.Models.AlertStateImage.Warning } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Critical) 
           { @PAL.Intranet.Models.AlertStateImage.Error } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Error) 
           { @PAL.Intranet.Models.AlertStateImage.Error } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Unknown) 
           { @PAL.Intranet.Models.AlertStateImage.Unknown } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Online) 
           { @PAL.Intranet.Models.AlertStateImage.OK } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Offline) 
           { @PAL.Intranet.Models.AlertStateImage.Error }"         
          width="24" height="24"/></td> 
       <td><b>@Model[i].Item:</b></td> 
       <td style="padding:0 0 0 15px;">@Model[i].TaskItem.ToString():&nbsp;&nbsp;</td> 
       <td>@if (@Model[i].Output != null) 
        { @Model[i].Output } 
        else 
        { @Model[i].AlertState.ToString() }</td> 
       <td style="padding:0 0 0 15px;">Captured:&nbsp;&nbsp;</td> 
       <td>@Model[i].Captured</td> 

       @if (i % 5 == 0) 
       { 
        </td> 
       } 

      </tr> 
     </table> 
    } 
</div> 

更新1

仍在期待}。我想在自己的桌子上每5件物品。

@model IList<PAL.Intranet.Models.MonitorDetailModel> 
<div> 
    @for (int i = 0; i < @Model.Count(); i++) 
    { 
     @if (i == 0 | i % 5 == 0) 
     { 
      <table> 
      <tr> 
     } 
      <td><img src="@if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.OK) 
          { @PAL.Intranet.Models.AlertStateImage.OK } 
          else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Alert) 
          { @PAL.Intranet.Models.AlertStateImage.Alert } 
          else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Warning) 
          { @PAL.Intranet.Models.AlertStateImage.Warning } 
          else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Critical) 
          { @PAL.Intranet.Models.AlertStateImage.Error } 
          else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Error) 
          { @PAL.Intranet.Models.AlertStateImage.Error } 
          else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Unknown) 
          { @PAL.Intranet.Models.AlertStateImage.Unknown } 
          else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Online) 
          { @PAL.Intranet.Models.AlertStateImage.OK } 
          else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Offline) 
          { @PAL.Intranet.Models.AlertStateImage.Error }"         
         width="24" height="24"/></td> 
      <td><b>@Model[i].Item:</b></td> 
      <td style="padding:0 0 0 15px;">@Model[i].TaskItem.ToString():&nbsp;&nbsp;</td> 
      <td>@if (@Model[i].Output != null) 
       { @Model[i].Output } 
       else 
       { @Model[i].AlertState.ToString() }</td> 
      <td style="padding:0 0 0 15px;">Captured:&nbsp;&nbsp;</td> 
      <td>@Model[i].Captured</td> 

      @if (i == 0 | i % 5 == 0) 
      { 
       </tr> 
       </table> 
      } 
    } 
</div> 

更新3

期待}在1号线,1,3,39,39

@model IList<PAL.Intranet.Models.MonitorDetailModel> 
<div>  
    @for (int i = 0; i < @Model.Count(); i++) 
    { 
     if (string.IsNullOrWhiteSpace(@Model[i].Output)) 
     { 
      Model[i].Output = @Model[i].AlertState.ToString(); 
     } 

     string image = string.Empty; 
     switch (@Model[i].AlertState) 
     { 
      case PAL.Intranet.Models.AlertState.OK: 
       image = PAL.Intranet.Models.AlertStateImage.OK; 
       break; 
      case PAL.Intranet.Models.AlertState.Warning: 
       image = PAL.Intranet.Models.AlertStateImage.Warning; 
       break; 
      case PAL.Intranet.Models.AlertState.Critical: 
       image = PAL.Intranet.Models.AlertStateImage.Error; 
       break; 
      case PAL.Intranet.Models.AlertState.Error: 
       image = PAL.Intranet.Models.AlertStateImage.Error; 
       break; 
      case PAL.Intranet.Models.AlertState.Unknown: 
       image = PAL.Intranet.Models.AlertStateImage.Unknown; 
       break; 
      case PAL.Intranet.Models.AlertState.Online: 
       image = PAL.Intranet.Models.AlertStateImage.OK; 
       break; 
      case PAL.Intranet.Models.AlertState.Offline: 
       image = PAL.Intranet.Models.AlertStateImage.Error; 
       break; 
      default: 
       image = PAL.Intranet.Models.AlertStateImage.Unknown; 
       break; 
     } 

     @if (i % 5 == 0) 
     { 
      <table>   
     } 
     <tr> 
     <td><img src="@image" width="24" height="24"/></td> 
     <td><b>@Model[i].Item:</b></td> 
     <td style="padding:0 0 0 15px;">@Model[i].TaskItem.ToString():&nbsp;&nbsp;</td> 
     <td>@Model[i].Output</td> 
     </tr> 
     @if (i % 5 == 0) 
     { 
      </table> 
     } 
    } 
</div> 
+0

有没有更好的方法?仍然在学习MVC,但希望能够轻松编辑,以便有人可以更改chtml文件,而不是加载VS来更改任何其他代码。 – Tsukasa 2015-03-31 12:49:09

+0

@JAT是我希望在自己的表格中收集每5个项目,因此我可以并排显示这些表格。 – Tsukasa 2015-03-31 13:06:06

+0

你确定你不只是从其他触发它的代码块中遗漏了一个'}'? – maam27 2015-03-31 13:57:53

回答

0

“问题”与您的代码它是剃刀试图找到表元素的结束标记,因为你实际上从来没有关闭具有良好的语法,它表明你在for循环和表标记中缺少右括号。正如你所说,如果你删除了表格标签效果很好,那么为了避免剃须刀试图完成标签元素,因为“错误”告诉​​你,这是因为你没有结束标签,只需在<table></table>之前添加@:;问题应该消失。

当然,你需要确保你的HTML代码是好的,因为你是在告诉剃须刀,这是一个字符串,而不是一个表,但在HTML它会呈现为<table>标签

@model IList<PAL.Intranet.Models.MonitorDetailModel> 
    <div>  
     @for (int i = 0; i < @Model.Count(); i++) 
     { 
      if (string.IsNullOrWhiteSpace(@Model[i].Output)) 
      { 
       Model[i].Output = @Model[i].AlertState.ToString(); 
      } 

      string image = string.Empty; 
      switch (@Model[i].AlertState) 
      { 
       case PAL.Intranet.Models.AlertState.OK: 
        image = PAL.Intranet.Models.AlertStateImage.OK; 
        break; 
       case PAL.Intranet.Models.AlertState.Warning: 
        image = PAL.Intranet.Models.AlertStateImage.Warning; 
        break; 
       case PAL.Intranet.Models.AlertState.Critical: 
        image = PAL.Intranet.Models.AlertStateImage.Error; 
        break; 
       case PAL.Intranet.Models.AlertState.Error: 
        image = PAL.Intranet.Models.AlertStateImage.Error; 
        break; 
       case PAL.Intranet.Models.AlertState.Unknown: 
        image = PAL.Intranet.Models.AlertStateImage.Unknown; 
        break; 
       case PAL.Intranet.Models.AlertState.Online: 
        image = PAL.Intranet.Models.AlertStateImage.OK; 
        break; 
       case PAL.Intranet.Models.AlertState.Offline: 
        image = PAL.Intranet.Models.AlertStateImage.Error; 
        break; 
       default: 
        image = PAL.Intranet.Models.AlertStateImage.Unknown; 
        break; 
      } 

      if (i % 5 == 0) 
      { 
       @:<table>   
      } 
      <tr> 
      <td><img src="@image" width="24" height="24"/></td> 
      <td><b>@Model[i].Item:</b></td> 
      <td style="padding:0 0 0 15px;">@Model[i].TaskItem.ToString():&nbsp;&nbsp;</td> 
      <td>@Model[i].Output</td> 
      </tr> 
      if (i % 5 == 0) 
      { 
       @:</table> 
      } 
     } 
    </div> 

我知道这是一个很好的解决方案,但我的一位朋友曾经这样做过,这就是他解决这个问题的方法。希望这可以帮助

1

您尝试添加一个TD每5个项目,但在如果你是triyng列来初始化一个tr,并最终尝试关闭一个td。根据发布的代码有一个标签不匹配。你可以尝试下面的代码吗?

@for (int i = 0; i < @Model.Count(); i++) 
    { 
     <table> 
      <tr> 
       @if (i % 5 == 0) 
       { 
        <td> 
       } 

       <td><img src="@if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.OK) 
           { @PAL.Intranet.Models.AlertStateImage.OK } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Alert) 
           { @PAL.Intranet.Models.AlertStateImage.Alert } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Warning) 
           { @PAL.Intranet.Models.AlertStateImage.Warning } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Critical) 
           { @PAL.Intranet.Models.AlertStateImage.Error } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Error) 
           { @PAL.Intranet.Models.AlertStateImage.Error } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Unknown) 
           { @PAL.Intranet.Models.AlertStateImage.Unknown } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Online) 
           { @PAL.Intranet.Models.AlertStateImage.OK } 
           else if (@Model[i].AlertState == PAL.Intranet.Models.AlertState.Offline) 
           { @PAL.Intranet.Models.AlertStateImage.Error }"         
          width="24" height="24"/></td> 
       <td><b>@Model[i].Item:</b></td> 
       <td style="padding:0 0 0 15px;">@Model[i].TaskItem.ToString():&nbsp;&nbsp;</td> 
       <td>@if (@Model[i].Output != null) 
        { @Model[i].Output } 
        else 
        { @Model[i].AlertState.ToString() }</td> 
       <td style="padding:0 0 0 15px;">Captured:&nbsp;&nbsp;</td> 
       <td>@Model[i].Captured</td> 

       @if (i % 5 == 0) 
       { 
        </td> 
       } 

      </tr> 
     </table> 
    } 
+0

糟糕,但它仍然给我一个错误,并且抱怨下面的错误没有起始标记,但我认为这是正常的,因为起始标记是在另一个如果语句 – Tsukasa 2015-03-31 12:57:30

+0

如果我删除

从if语句中的问题消失,但现在我没有我的表lol – Tsukasa 2015-03-31 14:11:45

+0

为什么不把两个td在相同的if语句?因为无论如何,如果我没有错,你只是试图为每个第五项创建一个td。使用目前的格式,它会将所有的TD包装在每五个物品的另一个TD中。 – 2015-03-31 14:24:07

1

您可以将剃刀块与开关盒组合使用。

@{ 
    string imageSource = string.Empty; 
    switch (Model[i].AlertState) { 
     case PAL.Intranet.Models.AlertState.OK: 
      imageSource = PAL.Intranet.Models.AlertStateImage.OK ; 
      break; 
     case PAL.Intranet.Models.AlertState.Alert: 
      imageSource = PAL.Intranet.Models.AlertStateImage.Alert 
      break; 
     ... 
    } 
} 

<td><img src="@imageSource" width="24" height="24"/></td> 

编辑

使用

@model IList<PAL.Intranet.Models.MonitorDetailModel> 
<div> 
    @for (int i = 0; i < @Model.Count(); i++) 
    { 
     string imageSource = string.Empty; 
     switch (Model[i].AlertState) { 
      case PAL.Intranet.Models.AlertState.OK: 
       imageSource = PAL.Intranet.Models.AlertStateImage.OK ; 
       break; 
      case PAL.Intranet.Models.AlertState.Alert: 
       imageSource = PAL.Intranet.Models.AlertStateImage.Alert 
       break; 
      ... 
     } 

     @if (i % 5 == 0) 
     { 
      <table>   
     } 
     <tr> 
      <td><img src="@imageSource" width="24" height="24"/></td> 
      <td><b>@Model[i].Item:</b></td> 
      <td style="padding:0 0 0 15px;">@Model[i].TaskItem.ToString():&nbsp;&nbsp;</td> 
      <td>@(Model[i].Output != null ? @Model[i].Output :@Model[i].AlertState.ToString())</td> 
      <td>@Model[i].Captured</td> 
     </tr> 

     @if (i % 5 == 0) 
     {    
      </table> 
     } 
    } 
</div> 
+0

Doh我不知道为什么我没有想到这一点。看起来很干净。 仍在期待}。更新代码 – Tsukasa 2015-03-31 13:26:05

+0

@Tsukasa,查看更新回答 – Satpal 2015-03-31 13:35:49

+0

同一问题更新代码 – Tsukasa 2015-03-31 13:51:56