2015-03-31 67 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