Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

asp.net mvc - 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....

question from:https://stackoverflow.com/questions/17772553/razor-html-raw-does-not-output-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...