Reputation: 19118
How can I add html to the page from inside an if()
in a @foreach()
. I get dont get any specific error it just does not take the second if()
. It writes it out like text.
this is what i tried but with no luck
@foreach (var item in Model)
{
if (count % 4 == 0 || totaltCount == 1)
{
<div class="in-instructor-container">
}
<div class="in-instructor">
<h3>@item.Name</h3>
@item.Information
</div>
if ((count - 1) % 3 == 0 || count == totaltCount) {
</div>
}
count++;
}
my html get like this
<div class="in-instructor-container">
}
<div class="in-instructor">
<h3>Test Person 0</h3>
bla bla bla bla bla bla bla
</div>
if ((count - 1) % 3 == 0 || count == totaltCount) {
</div>
Upvotes: 49
Views: 45595
Reputation: 28625
You need to add @: in front of the html inside the if statements:
@foreach (var item in Model)
{
if (count % 4 == 0 || totaltCount == 1)
{
// Single line html
@:<div class="in-instructor-container">
// Multi-line html
<text>
Your html code here
</text>
}
<div class="in-instructor">
<h3>@item.Name</h3>
@item.Information
</div>
if ((count - 1) % 3 == 0 || count == totaltCount) {
@:</div>
}
count++;
}
I found this solution from this stackoverflow question
Upvotes: 112