剃须刀- HTML。RAW格式不输出文本

时间:2022-07-26 22:28:43

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:

我尝试过所有针对其他类似问题提出的解决方案,但似乎都不管用。本质上,我试图显示一个表格,其中包含来自模型集合的数据。这本身并不是问题,但是我想强制razor总是在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....

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

5 个解决方案

#1


61  

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:

之所以没有输出,是因为执行了razor语法的上下文。在if块中,所有代码运行时都像在常规c#上下文中一样:

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

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

返回一个MvcHtmlString,但是您没有使用它做任何事情。您需要输入输出上下文:

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

#2


3  

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.

希望它可以帮助。

#3


0  

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.

我希望它有帮助。

#4


0  

Html.Raw

Html.Raw

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

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

Controller

控制器

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

output

输出

@html.raw(viewbag.message);

#5


0  

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 with @(和)为我解决了这个问题。即使有外部@{和},它仍然需要@(和)围绕每个Html。原始的声明。

#1


61  

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:

之所以没有输出,是因为执行了razor语法的上下文。在if块中,所有代码运行时都像在常规c#上下文中一样:

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

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

返回一个MvcHtmlString,但是您没有使用它做任何事情。您需要输入输出上下文:

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

#2


3  

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.

希望它可以帮助。

#3


0  

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.

我希望它有帮助。

#4


0  

Html.Raw

Html.Raw

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

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

Controller

控制器

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

output

输出

@html.raw(viewbag.message);

#5


0  

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 with @(和)为我解决了这个问题。即使有外部@{和},它仍然需要@(和)围绕每个Html。原始的声明。