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

时间:2013-09-11 19:36:11

标签: c# xml asp.net-mvc linq-to-xml

我有一个包含多个属性的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>

控制器

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 });

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

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

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

@Html.EscapeHTML(Model.Property)