Html.Raw()在ASP.NET MVC中的Razor视图 [英] Html.Raw() in ASP.NET MVC Razor view

查看:325
本文介绍了Html.Raw()在ASP.NET MVC中的Razor视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@{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++)

}

这code部分不编译,并出现以下错误

This code part does not compile, with the following error

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

我要我做什么呢?谢谢你。

What I must I do? Thanks.

推荐答案

Html.Raw()收益 IHtmlString ,不是普通的字符串。 运营商:所以,你不能在的两侧写。删除的ToString()呼叫

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("<div class=\"resource-row\">"): Html.Raw("")) 
    // some code
    @(count <= 3 ? Html.Raw("</div>") : Html.Raw("")) 
    @(count++)

}

顺便说一句,返回 IHtmlString 是MVC承认HTML内容,并且不带code的方式。即使没有造成编译器错误,称的ToString()将破坏) Html.Raw意义(

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()

这篇关于Html.Raw()在ASP.NET MVC中的Razor视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆