Razor - HTML.RAW 不输出文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17772553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:23:35  来源:igfitidea点击:

Razor - HTML.RAW does not output text

htmlasp.net-mvcrazor

提问by Palkos

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:

我已经尝试了针对其他类似问题提出的所有解决方案,但似乎都没有奏效。从本质上讲,我试图显示一个充满来自模型集合的数据的表格。这本身不是问题,但是我想强制剃刀总是在 3 列中生成它(无论我们有多少元素)。我最初的想法是这样做:

 <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

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

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

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

实际被调用,但在生成的页面中不存在输出。

Any help? Many thanks in advance....

有什么帮助吗?提前谢谢了....

回答by Simon Belanger

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

它不输出的原因是因为正在执行的 razor 语法的上下文。在您的if块中,所有代码都像在常规 C# 上下文中一样运行,并且该行:

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

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

返回 anMvcHtmlString但你没有对它做任何事情。您需要输入输出上下文:

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

回答by ysrb

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.

希望能帮助到你。

回答by saurav singh

Html.Raw actually used for line break like what you do in c# using /n

Html.Raw 实际上用于换行,就像你在 c# 中使用 /n 所做的一样

For Example:

例如:

<text>
@html.raw("</tr><tr>")
</text>

I hope it helps.

我希望它有帮助。

回答by Rizwan Younas

Html.Raw

原始文件

Wraps HTML markup in an HtmlString instance so that it is interpreted as HTML markup. For Example :

将 HTML 标记包装在 HtmlString 实例中,以便将其解释为 HTML 标记。例如 :

Controller

控制器

public actionresult Htmlraw()
{
    viewbag.message = "Hey friends lets go" + "<br />" + "for chillout";
    return view();
}

output

输出

@html.raw(viewbag.message);

回答by Isaac Levy

Enclosing Html.Raw with @( and ) solved the issue for me. Eventhough there were outer @{ and } , it still needed @( and ) around each Html.Raw statement.

用 @( 和 ) 封闭 Html.Raw 为我解决了这个问题。即使有外部 @{ 和 } ,它仍然需要 @( 和 ) 围绕每个 Html.Raw 语句。