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

后端 未结 3 762
醉梦人生
醉梦人生 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:46

    Html.Raw() returns IHtmlString, not the ordinary string. So, you cannot write them in opposite sides of : operator. Remove that .ToString() calling

    @{int count = 0;}
    @foreach (var item in Model.Resources)
    {
        @(count <= 3 ? Html.Raw("
    "): Html.Raw("")) // some code @(count <= 3 ? Html.Raw("
    ") : Html.Raw("")) @(count++) }

    By the way, returning IHtmlString is the way MVC recognizes html content and does not encode it. Even if it hasn't caused compiler errors, calling ToString() would destroy meaning of Html.Raw()

提交回复
热议问题