开发者

HTML formatting tags being ignored in MVC2

开发者 https://www.devze.com 2023-02-14 01:44 出处:网络
I\'ve looked around and can\'t find a solution for this and it\'s driving me mad. I\'ve got a basic MVC2 app (C#) and am trying to display text from a database with the line breaks included. I\'ve us

I've looked around and can't find a solution for this and it's driving me mad.

I've got a basic MVC2 app (C#) and am trying to display text from a database with the line breaks included. I've used the following:

<p><label>Description:</label>
<span class="fieldBlock"><%: Model.Description.Replace(System.Environment.NewLine, "<br />") %></span>
</p>

This is adding the tags to the returned string as I'd expect but when the page renders it ac开发者_JAVA技巧tually displays the tag as is instead of rendering the newline. ie. the text seen by the user in the browser is:

"Quisque justo erat, iaculis sit amet aliquam eu, porttitor in mauris.<br /><br />Maecenas nisi velit, euismod at molestie vitae, malesuada id turpis. Mauris diam nisl, pretium id molestie nec, posuere posuere neque.<br /><br />Cras sed lectus nisl."

This is the same in all major browsers so I'm obviously doing something foolish, any help would be much appreciated.


Change <%: to <%=. This new notation encodes for HTML. The 2nd way is the old fashioned way.

0

精彩评论

暂无评论...
验证码 换一张
取 消