剃须刀 - HTML.RAW不输出文本 [英] Razor - HTML.RAW do not output text

查看:109
本文介绍了剃须刀 - HTML.RAW不输出文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试提出其他类似问题的解决方案都没有,但他们似乎工作。
从本质上讲,我想显示充斥着从模型收集数据的表。这本身不是一个问题,但是我想迫使剃刀在3列始终生成它(不管我们有多少元素都有)。我最初的想法是这样做的:

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>

因此​​,在本质每次我想剃刀输出三元素串到另一行添加到表中。一切似乎很好地工作比其他蜇这是不是在网页的源文件present。在调试,我可以看到这行

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

实际上是调用,但在生成的页面没有输出为present。

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

任何帮助吗?
提前许多感谢....

Any help? Many thanks in advance....

推荐答案

它不允许输出的原因是因为剃刀语法的上下文中被执行。在你的如果块,所有code运行,如果你是在正规C#背景和行:

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

返回 MvcHtmlString ,但你没有做任何事的。您需要输入输出方面:

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

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

这篇关于剃须刀 - HTML.RAW不输出文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆