2017-08-15 36 views
-1

我想通過一組數字循環並在記分卡上顯示它們。如果我逐句通過代碼,變量返回正確的值,我無法讓它們顯示。試圖在C#中使用剃鬚刀的for循環中顯示變量/ Linq

@foreach (var g in Model) 
    { 
     @Html.Raw(string.Format("<tr>")) 
      int grossScore = 0; 

      @Html.Raw(string.Format("<td>{0}</td>", g.PlayerName)) 
     for (int s = 0; s < 18; s++) 
     { 
      @Html.Raw(string.Format("<td>")); 

      int holescore = 0; 
      var bretak = @g.Scores.Take(1).Skip(s).FirstOrDefault(); 
      var werwee = @g.Scores.Where(b => b.Hole == s + 1).Select(i => i.Score).FirstOrDefault(); 
      //if (bretak.Score > 0) 
      //{ 
      // holescore = bretak.Score; 
      //} 

      holescore = werwee; 

      Html.Raw(string.Format("{0}", holescore)); 
      //@Html.ToString(holescore); 
      //Html.Raw(holescore); 
      @Html.Raw(string.Format("<td>" + @holescore + "</td>")); 
      grossScore = grossScore + holescore; 

      @Html.Raw(string.Format("</td>")); 
     } 
      @Html.Raw(string.Format("<td>{0}</td>", grossScore)) 

      @Html.Raw(string.Format("<td align=\"center\"> <button type=\"button\" class=\"btn btn-default btn-xs editBtn\" data-player-id=\"'{0}'\">Edit </button></td>", g.GameId)) 
      @Html.Raw(string.Format("<td> <button type=\"button\" class=\"btn btn-default btn-xs deleteBtn\" data-player-id=\"'{0}'\">Delete </button></td>", g.GameId)) 
     @Html.Raw("</tr>") 
    } 

玩家名稱正確顯示,grossscore變量可以正確計算和顯示。我從循環中獲得正確數量的盒子,但holecore不會顯示。對不起,它可能看起來有點混亂,但我留下了一些代碼來展示我嘗試過的一些東西。

+0

'開始添加@ holecore'是通過'holescore = werwee;'設置的,所以確保'werwee'不是'null' – CodeNotFound

+0

它在第四洞後是空的,但是洞1-4都有有效的數據。 –

+0

如果你想顯示它,那麼使用' @ holescore'。 – CodeNotFound

回答

1

我想你是誤會了html.Raw,它是用來串輸出解碼成HTML字符串,而不是,輸出只需輸入 所以不是這樣

@Html.Raw(string.Format("<td>" + @holescore + "</td>")); 

東西只是用這個

<td> @holescore</td> 

我建議你通過this去得到一個更清晰的圖片,因爲我假設你不是基於上面的代碼。

+0

謝謝你。你的代碼也可以工作,但是我做了上面提到的同樣的事情。如果我點擊調試,我的網站將無法加載。它會坐在一個空白的屏幕上,直到我點擊然後會出來工作。關於​​的有趣之處在於它下面有一條扭曲的線條,表示「元素'td'不能嵌套在元素'table'內部.td元素表示表格中的數據單元格。」任何想法爲什麼會這樣做? –

+0

你應該刪除html.raw,這是不必要的,我認爲你錯過了你的tr標籤,這就是爲什麼你收到這條消息。仔細檢查你的''標籤 – Munzer

0

克里斯在看完你的代碼之後,聲明需要@在開始時。

Html.Raw(string.Format("{0}", holescore)); 

但低於語句應該具有顯示score.Have你嘗試過這種說法或其只是信息

@Html.Raw(string.Format("<td>" + @holescore + "</td>")); 

要麼使用上面的語句,或在第一條語句

相關問題