在C#Razor中将字符串呈现为HTML [英] Render a string as HTML in C# Razor

查看:59
本文介绍了在C#Razor中将字符串呈现为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从模型中渲染地址.该字符串包含换行符,我将使用break标记替换换行符.虽然,它以字符串而不是HTML的形式呈现在页面上.如何强制将字符串呈现为HTML?

I am attempting to render an address from my model. The string contains line breaks that I am replacing with a break tag. Although, it is rendering on the page as a string instead as HTML. How can I force my string to render as HTML instead?

尝试:

<span id="addressLine">
    @Model.MyData.Address.Replace("\r\n", "<br />");
</span>

页面结果:

Address Top<br />Street Name<br />City<br />PostCode

应显示为:

Address Top
Street Name
City
PostCode

推荐答案

使用@Html.Raw(Model.MyData.Address.Replace("\r\n", "<br />"))

这篇关于在C#Razor中将字符串呈现为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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