Razor - HTML.RAW does not output text

前端 未结 5 1544
情话喂你
情话喂你 2020-12-09 14:45

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

相关标签:
5条回答
  • 2020-12-09 15:23

    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.

    0 讨论(0)
  • 2020-12-09 15:23

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

    For Example:

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

    I hope it helps.

    0 讨论(0)
  • 2020-12-09 15:30

    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>");
    
    0 讨论(0)
  • 2020-12-09 15:38

    Html.Raw

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

    Controller

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

    output

    @html.raw(viewbag.message);
    
    0 讨论(0)
  • 2020-12-09 15:38

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

    0 讨论(0)
提交回复
热议问题