Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor - HTML.RAW does not output text

I have tried all solution proposed to other, similar questions but none of them seems to work. In essence I am trying to display a table filled with data from collection of models. That in itself is not a problem, however I would like to force razor to generate it always in 3 columns (no matter how many elements we have). My original idea was to do it that way:

 <table class="projects-grid">     <tr>     @for(int i = 0; i< Model.Count(); i++)       {          if (i != 0 && i % 3 == 0)           {              Html.Raw("</tr><tr>");          }         var item = Model.ElementAt(i);         <td class="project-tile">              @Html.DisplayFor(modelItem => item.Title)                         </td>             }     </tr>     </table> 

So in essence every third element I would like Razor to output "" string to add another row to the table. All seems to work fine other than this sting is not present in page source. In debug I can see that this line

 Html.Raw("</tr><tr>"); 

Is actually called, but no output in generated page is present.

Any help? Many thanks in advance....

like image 512
Palkos Avatar asked Jul 21 '13 12:07

Palkos


People also ask

Why not use HTML Raw?

Raw can result in a XSS vulnerability being exploitable since an attacker can craft a special URL containing a malicious JavaScript payload that will be executed by the victim's browser if he or she sends an invalid 2FA confirmation code.

What does HTML raw do C#?

Raw allows you to output text containing html elements to the client, and have them still be rendered as such. Should be used with caution, as it exposes you to cross site scripting vulnerabilities.

Can we use HTML Raw?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. In this article I will explain with an example, how to use Html.

What is @model in razor?

New @model directive The @model directive provides a cleaner and more concise way to reference strongly-typed models from view files.


2 Answers

The reason it's not outputing is because of the context of the razor syntax being executed. In your if block, all code runs as if you were in a regular C# context and the line:

Html.Raw("</tr><tr>"); 

Returns an MvcHtmlString but you are not doing anything with it. You need to enter an output context:

@Html.Raw("</tr><tr>"); 
like image 80
Simon Belanger Avatar answered Oct 06 '22 07:10

Simon Belanger


I would use a work around.

Try:

<table class="projects-grid">     <tr>     @for(int i = 0; i< Model.Count(); i++)       {          if (i != 0 && i % 3 == 0)           {              <text>              @Html.Raw("</tr><tr>")              </text>          }         var item = Model.ElementAt(i);         <td class="project-tile">              @Html.DisplayFor(modelItem => item.Title)                         </td>             }     </tr>     </table> 

Hope it helps.

like image 30
ysrb Avatar answered Oct 06 '22 06:10

ysrb