Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add html to the page inside a Razor if statement in a foreach?

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> 
like image 321
Dejan.S Avatar asked Mar 21 '12 18:03

Dejan.S


People also ask

What is HTML Razor view?

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.

Can you use Razor syntax in JavaScript?

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.

What is HTML raw in MVC?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor.

How do I debug a .razor page?

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.


1 Answers

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

like image 172
Josh Mein Avatar answered Sep 18 '22 23:09

Josh Mein