Html.Raw() in ASP.NET MVC Razor view

后端 未结 3 761
醉梦人生
醉梦人生 2020-12-01 07:14
@{int count = 0;}
@foreach (var item in Model.Resources)
{
    @(count <= 3 ? Html.Raw("
").ToString() : Ht
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 07:45

    You shouldn't be calling .ToString().

    As the error message clearly states, you're writing a conditional in which one half is an IHtmlString and the other half is a string.
    That doesn't make sense, since the compiler doesn't know what type the entire expression should be.


    There is never a reason to call Html.Raw(...).ToString().
    Html.Raw returns an HtmlString instance that wraps the original string.
    The Razor page output knows not to escape HtmlString instances.

    However, calling HtmlString.ToString() just returns the original string value again; it doesn't accomplish anything.

提交回复
热议问题