ASP.NET MVC - 使用XDocument - br标签在屏幕上显示为文本

时间:2022-11-12 00:21:20

I have an XML document which contains multiple properties and each property has a summary description node. The summary description nodes sometimes contain html
tags that I'd like to be used to actually create a line break on the page, but the
tags are currently being displayed as text. I'm currently using XDocument in the contr to grab the value of the summaryDescription node and then passing this to the view via a view model.

我有一个包含多个属性的XML文档,每个属性都有一个摘要描述节点。摘要描述节点有时包含我希望用于在页面上实际创建换行符的html标记,但标记当前显示为文本。我目前正在控件中使用XDocument来获取summaryDescription节点的值,然后通过视图模型将其传递给视图。

XML

<summaryDescription>
A very spacious first floor four bedroom apartment situated in a convenient location. The apartment has a very impressive accommodation comprising main entrance foyer leading to hall, formal lounge, well appointed kitchen breakfast area and TV common room, family bathroom and en-suite shower room.<br/><br/>
</summaryDescription>

Controller

var query = (from props in xml.Descendants("property")
             select new PropertyResult
             {
                 Description = props.Element("summaryDescription").Value,
                 PriceText = props.Element("pricetext").Value,
                 Image = "http://www.dezrez.com/estate-agent-software/ImageResizeHandler.do?&photoID=1&AgentID=1239&BranchID=1976&Width=500&PropertyId=",
              }).ToArray();

return View(new ResultsViewModel { Property = query });

As you can see the summaryDescription xml node has two line breaks at the end but these are currently being displayed as text on the page. If anyone could help me with forcing the page to see the
tags as html tags, I'd be very grateful.

如您所见,summaryDescription xml节点在结尾处有两个换行符,但这些换行符当前在页面上显示为文本。如果有人可以帮我强迫页面将标签看作html标签,我将非常感激。

Thanks in advance!

提前致谢!

1 个解决方案

#1


0  

In your razor view markup, use this Extension method, on your property.

在剃须刀视图标记中,在您的属性上使用此扩展方法。

public static IHtmlString EscapeHTML(this HtmlHelper htmlHelper, string value) { return new HtmlString(value.ToString()); }

public static IHtmlString EscapeHTML(this HtmlHelper htmlHelper,string value){return new HtmlString(value.ToString()); }

@Html.EscapeHTML(Model.Property)

#1


0  

In your razor view markup, use this Extension method, on your property.

在剃须刀视图标记中,在您的属性上使用此扩展方法。

public static IHtmlString EscapeHTML(this HtmlHelper htmlHelper, string value) { return new HtmlString(value.ToString()); }

public static IHtmlString EscapeHTML(this HtmlHelper htmlHelper,string value){return new HtmlString(value.ToString()); }

@Html.EscapeHTML(Model.Property)