I am a junior web developer and this is my first time posting here.
I am having a problem where for some reason the most inner if statement is not recognized and is instead printed as text. Anyone got a solution for this?
EDIT: Found a solution: 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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With