asp.net-mvc ASP.NET MVC Razor 视图中的 Html.Raw()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7027683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:24:20  来源:igfitidea点击:

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

asp.net-mvcrazor

提问by Artur Keyan

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

}

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.

我必须做什么?谢谢。

回答by archil

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

Html.Raw()回报IHtmlString,不平凡string。因此,您不能将它们写在:运算符的两侧。删除那个.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++)

}

By the way, returning IHtmlStringis 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()

顺便说一句,返回IHtmlString是MVC识别html内容而不对其进行编码的方式。即使它没有导致编译器错误,调用ToString()也会破坏Html.Raw()

回答by rfmodulator

The accepted answer is correct, but I prefer:

接受的答案是正确的,但我更喜欢:

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

I hope this inspires someone, even though I'm late to the party.

我希望这能激励某人,即使我迟到了。

回答by SLaks

You shouldn't be calling .ToString().

你不应该打电话.ToString()

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

正如错误消息明确指出的那样,您正在编写一个条件,其中一半是 an IHtmlString,另一半是字符串。
这没有意义,因为编译器不知道整个表达式应该是什么类型。



There is nevera reason to call Html.Raw(...).ToString().
Html.Rawreturns an HtmlStringinstance that wraps the original string.
The Razor page output knows not to escape HtmlStringinstances.

从来没有一个理由骂Html.Raw(...).ToString()
Html.Raw返回一个HtmlString包装原始字符串的实例。
Razor 页面输出知道不转义HtmlString实例。

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

但是,调用HtmlString.ToString()只是string再次返回原始值;它没有完成任何事情。