@{int count = 0;} @foreach (var item in Model.Resources) { @(count <= 3 ? Html.Raw("<div class=\"resource-row\">").ToString() : Html.Raw("")) // some code @(count <= 3 ? Html.Raw("</div>").ToString() : Html.Raw("")) @(count++) }
Error 18 Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'System.Web.IHtmlString' d:\Projects\IRC2011_HG\IRC2011\Views\Home\_AllResources.cshtml 21 24 IRC2011
我必须做什么?谢谢。
解决方法
Html.Raw()返回IHtmlString,而不是普通字符串。所以,你不能将它们写在:运算符的对面。删除.ToString()调用
@{int count = 0;} @foreach (var item in Model.Resources) { @(count <= 3 ? Html.Raw("<div class=\"resource-row\">"): Html.Raw("")) // some code @(count <= 3 ? Html.Raw("</div>") : Html.Raw("")) @(count++) }
顺便说一下,返回IHtmlString是MVC识别html内容和不编码的方式。即使它没有导致编译器错误,调用ToString()会破坏Html.Raw()的意思,