views:

178

answers:

2

Is there some magic existing code in MVC 2 to Html.Encode() strings and allow certain html markup, like paragraph marks and breaks? (coming from a Linq to SQL database field)

A horrible code example to achieve the effect:

Html.Encode(Model.fieldName).Replace("&lt;br /&gt;", "<br />")

What would be really nice is to overload something and pass to it an array (or object) full of allowed html tags.

+1  A: 

I can't think of anything off the bat but I guess you could write an extension method that allows you to add a paremeter/list of items to allow.

Html.Encode(Mode.fieldName, List<items> Myitems);

It could modify the allowable tags into &lt; etc and then encodes the rest like normal.

griegs
The only problem with going like this is that if someone explicitly puts `<br/>` in their HTML (expecting it to display like that, rather than being converted to an actual new-line) will be surprised when it gets "unescaped" by your routine...
Dean Harding
+2  A: 

There is nothing built in to ASP.NET or MVC for this, but it's not that hard to write your own whitelist-based one with regular expressions and so on. Here's one that Jeff wrote, though it's pretty rough around the edges...

Dean Harding