2012-07-18 91 views
3

我是初級Web開發人員,這是我第一次在這裏發佈。ASP.NET MVC 3 Razor嵌套foreach with if語句

我有一個問題,因爲某些原因最內在的if語句不被識別,而是被打印爲文本。任何人都有這個解決方案?

編輯:找到一個解決方案:Can't seem to conditionally create a new table row using Razor's foreach and if statements?

@model IEnumerable<FairShare.Models.Product> 
@{ 
    ViewBag.Title = "Products"; 
} 
<h2> 
    Auctions</h2> 
<table border="1"> 
    <col width="192" /> 

    @{int i = 0;} 
     @foreach (var item in Model) 
     { 
      if (item.DateEnd.Subtract(DateTime.Now).TotalMinutes > -5) 
      { 
       if (i == 0) 
       { 
        <tr> 
       } 
      <td> 
       <a href="/ProductDetails/[email protected]"> 
        <img src="Images/@item.ImageName" width="192" height="108"/> 
       </a> 
       <br /> 
       <a href="/ProductDetails/[email protected]">@Html.DisplayFor(modelItem => item.ShortTitle)</a><br /> 
       @Html.DisplayFor(modelItem => item.ShortDescription)<br /> 
       <div style="color: red;">@Html.DisplayFor(modelItem => item.TimeLeft)</div> 
       <div style="color: green;"> 
        Current bid: @Html.DisplayFor(modelItem => item.CurrentBid)</div> 
      </td> 

       i = i + 1; 
       if (i == 5) 
       { 
        </tr> 
        i = 0; 
       } 
      } 
     } 

</table> 

回答

8
  You need to write code this way. @Html.Raw("<tr>") 
      Copy the below code and paste it into your view. it will work. 

      @model IEnumerable<FairShare.Models.Product> 

      @{ 
       ViewBag.Title = "Products"; 
      } 
      <h2> 
       Auctions</h2> 
      <table border="1"> 
       <col width="192" /> 

       @{int i = 0;} 
        @foreach (var item in Model) 
        { 
         if (item.DateEnd.Subtract(DateTime.Now).TotalMinutes > -5) 
         { 
          if (i == 0) 
          { 
           @Html.Raw("<tr>") 
          } 
         <td> 
          <a href="/ProductDetails/[email protected]"> 
           <img src="Images/@item.ImageName" width="192" height="108"/> 
          </a> 
          <br /> 
          <a href="/ProductDetails/[email protected]">@Html.DisplayFor(modelItem => item.ShortTitle)</a><br /> 
          @Html.DisplayFor(modelItem => item.ShortDescription)<br /> 
          <div style="color: red;">@Html.DisplayFor(modelItem => item.TimeLeft)</div> 
          <div style="color: green;"> 
           Current bid: @Html.DisplayFor(modelItem => item.CurrentBid)</div> 
         </td> 


          i = i + 1; 
          if (i == 5) 
          { 
           @Html.Raw("</tr>") 
           i = 0; 
          } 
         } 
        } 

      </table> 
1

****道歉我的表情卻是有點笨,但工作原理是這樣的例子。您不需要額外的@內部代碼,並且使用html.raw變得非常方便!謹防缺少關閉標籤,訣竅在於將條件內容包裝在<text>標籤內;)根據以下示例:****

@{ 
     int x = 0; 
    } 

    @foreach (var item in Model) 
    { 

     if (x == 0) 
     { 
      <text> 
       @Html.Raw("<div id=\"rowbeg\" class=\"row\">") 
      </text> 
     } 

     <div class="col-md-3 col-sm-6 col-md-12"> 
      <div class="item"> 
       <!-- Use the below link to put HOT icon --> 
       <div class="item-icon"><span>HOT/SALE/50%</span></div> 
       <!-- Item image --> 
       <div class="item-image"> 
        @{ 
        var base64 = Convert.ToBase64String(item.ProductImage); 
        var imgSrc = String.Format("data:image/gif;base64,{0}", base64); 
        } 
        <img src='@imgSrc' style="max-width:200px; max-height:150px;" /> 
       </div> 
       <!-- Item details --> 
       <div class="item-details"> 
        <h5><a href="single-item.html" style="color:blue;">@item.ProductName</a></h5> 
        <div class="clearfix"></div> 
        <p>@item.ProductDescription</p> 
        <!-- Price --> 
        <div class="item-price pull-left">[email protected]</div> 
        <!-- Add to cart --> 
        <div class="pull-right"><a href="#" class="btn btn-success btn-sm">Add to Cart</a></div> 
        <div class="clearfix"></div> 
        <hr /> 
       </div> 
      </div> 
     </div> 

     x = x + 1; 
     if (x == 2) 
     { 
      x = 0; 
      <text> 
      @Html.Raw("</div>") 
      </text> 


     } 


    }