C# 在asp.net mvc视图中将字符串显示为html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19980657/
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-08-10 16:18:19  来源:igfitidea点击:

Display string as html in asp.net mvc view

c#asp.netasp.net-mvcasp.net-mvc-4

提问by Mukesh Sharma

I have a controller which generate string containing html markups.Now when I am displaying it on views, it is displayed as simple string containing all tags. I tried to use Html helper to encode/decode to display it properly, but it is not working.

我有一个控制器,它生成包含 html 标记的字符串。现在当我在视图上显示它时,它显示为包含所有标签的简单字符串。我尝试使用 Html helper 进行编码/解码以正确显示它,但它不起作用。

string str= "<a href="/Home/Profile/seeker">seeker</a> has applied to <a href="/Jobs/Details/9">Job</a> floated by you.</br>";

On my views,

在我看来,

@Html.Encode(str)

采纳答案by Jared

You are close you want to use @Html.Raw(str)

你很接近你想使用 @Html.Raw(str)

@Html.Encodetakes strings and ensures that all the special characters are handled properly. These include characters like spaces.

@Html.Encode接受字符串并确保正确处理所有特殊字符。这些包括空格等字符。

回答by AthibaN

you can use @Html.Raw(str)

您可以使用 @Html.Raw(str)

See MSDNfor more

查看MSDN了解更多

Returns markup that is not HTML encoded.

This method wraps HTML markup using the IHtmlString class, which renders unencoded HTML.

返回非 HTML 编码的标记。

此方法使用 IHtmlString 类包装 HTML 标记,该类呈现未编码的 HTML。

回答by Jerad Rose

You should be using IHtmlStringinstead:

你应该使用IHtmlString

IHtmlString str = new HtmlString("<a href="/Home/Profile/seeker">seeker</a> has applied to <a href="/Jobs/Details/9">Job</a> floated by you.</br>");

Whenever you have model properties or variables that need to hold HTML, I feel this is generally a better practice. First of all, it is a bit cleaner. For example:

每当您有需要保存 HTML 的模型属性或变量时,我觉得这通常是一种更好的做法。首先,它更干净一些。例如:

@Html.Raw(str)

Compared to:

相比:

@str

Also, I also think it's a bit safer vs. using @Html.Raw(), as the concern of whether your data is HTML is kept in your controller. In an environment where you have front-end vs. back-end developers, your back-end developers may be more in tune with what data can hold HTML values, thus keeping this concern in the back-end (controller).

另外,我也认为它比使用更安全一些@Html.Raw(),因为您的数据是否是 HTML 的问题保存在您的控制器中。在您拥有前端开发人员与后端开发人员的环境中,您的后端开发人员可能更了解哪些数据可以保存 HTML 值,从而将这种关注保留在后端(控制器)中。

I generally try to avoid using Html.Raw()whenever possible.

我通常尽量避免使用Html.Raw()

One other thing worth noting, is I'm not sure where you're assigning str, but a few things that concern me with how you may be implementing this.

另一件值得注意的事情是,我不确定你在哪里分配str,但有一些事情让我担心你可能如何实现它。

First, this should be done in a controller, regardless of your solution (IHtmlStringor Html.Raw). You should avoid any logic like this in your view, as it doesn't really belong there.

首先,这应该在控制器中完成,而不管您的解决方案(IHtmlStringHtml.Raw)。在您看来,您应该避免任何类似的逻辑,因为它并不真正属于那里。

Additionally, you should be using your ViewModelfor getting values to your view (and again, ideally using IHtmlStringas the property type). Seeing something like @Html.Encode(str)is a little concerning, unless you were doing this just to simplify your example.

此外,您应该使用您的ViewModel来获取视图的值(再次,理想情况下IHtmlString用作属性类型)。看到类似的东西@Html.Encode(str)有点令人担忧,除非你这样做只是为了简化你的例子。

回答by Roland Wales

I had a similar problem with HTML input fields in MVC. The web paged only showed the first keyword of the field. Example: input field: "The quick brown fox" Displayed value: "The"

我在 MVC 中的 HTML 输入字段也有类似的问题。网页只显示该字段的第一个关键字。示例:输入字段:“The quick brown fox” 显示值:“The”

The resolution was to put the variable in quotes in the value statement as follows:

解决方法是将变量放在 value 语句中的引号中,如下所示:

<input class="ParmInput" type="text" id="respondingRangerUnit" name="respondingRangerUnit"
       onchange="validateInteger(this.value)" value="@ViewBag.respondingRangerUnit">

回答by Greg

I had a similar problemrecently, and google landed me here, so I put this answer here in case others land here as well, for completeness.

我最近遇到了类似的问题,谷歌把我带到了这里,所以为了完整性,我把这个答案放在这里以防其他人也在这里登陆。

I noticed that when I had badly formatted html, I was actually having all my html tags stripped out, with just the non-tag content remaining. I particularly had a table with a missing opening table tag, and then all my html tags from the entire string where ripped out completely.

我注意到,当我的html 格式错误时,实际上我的所有 html 标签都被剥离了只剩下非标签内容。我特别有一个缺少开始表标签的表格,然后我的整个字符串中的所有 html 标签都被完全撕掉了。

So, if the above doesn't work, and you're still scratching your head, then also check you htmlfor being valid.

因此,如果上述方法不起作用,并且您仍在挠头,那么还要检查您的 html是否有效。

I notice even after I got it working, MVC was adding tbody tags where I had none. This tells me there is clean up happening (MVC 5), and that when it can't happen, it strips out all/some tags.

我注意到即使在我开始工作之后,MVC 还在我没有的地方添加了 tbody 标签。这告诉我有清理发生(MVC 5),当它不能发生时,它会删除所有/一些标签。