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>
Razor is a standard markup syntax that allows us to embed server code into the web pages. It uses its own syntax and keywords to generate view. If there is server code in the web page, server executes that code first then send response to the browser. It allows us to perform logical tasks in the view page.
You can't. Razor is a . NET assembly and doesn't run on JavaScript or in a browser. It's meant to be executed server-side.
The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor.
To debug, you need to find out what's happening to the value of key objects and variables such as weekday . Add output expressions by inserting @weekday as shown in the two places indicated by comments in the code. These output expressions will display the values of the variable at that point in the code execution.
You need to add @: in front of the html inside the if statements:
@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++; }
I found this solution from this stackoverflow question
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