Razor - HTML.RAW不输出文本 [英] Razor - HTML.RAW does not output text

查看:324
本文介绍了Razor - 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>

所以实质上每个第三个元素我都希望Razor输出字符串来添加另一行到表。所有似乎工作正常,除了这种刺痛不存在于页面源。在调试中我可以看到这一行

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....

推荐答案

不输出的原因是因为剃须刀的上下文正在执行的语法。在你的 if 块中,所有的代码运行,就像你在一个普通的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>");

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

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