4

我是一名初级网络开发人员,这是我第一次在这里发帖。

我遇到了一个问题,由于某种原因,最内部的 if 语句无法识别,而是打印为文本。有人对此有解决方案吗?

编辑:找到解决方案:似乎无法使用 Razor 的 foreach 和 if 语句有条件地创建新表行?

@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/Index?productID=@item.ID">
                    <img src="Images/@item.ImageName" width="192" height="108"/>
                </a>
                <br />
                <a href="/ProductDetails/Index?productID=@item.ID">@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>
4

2 回答 2

9
          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/Index?productID=@item.ID">
                                <img src="Images/@item.ImageName" width="192" height="108"/>
                            </a>
                            <br />
                            <a href="/ProductDetails/Index?productID=@item.ID">@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>
于 2012-07-18T20:12:34.870 回答
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">$@item.ProductPrice</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>


        }


    }
于 2016-02-15T18:32:45.973 回答