Why is the line break tag displaying as text from a Custom Html Helper asp.net mvc
I have an html helper which takes in a broken address and formats it, but i'm seeing the actual <br />
tag as text instead of a line break on my web page.
What am i doing that's causing this to occur?
Here's part of my helper method
public static HtmlFormatAddress(this helper, string number
, string fraction
, string direction
, string street
, string type
....)
{
var sb = new StringBuilder();
if (!开发者_开发问答string.IsNullOrEmpty(number))
sb.Append(number.Trim() + " ");
if (!string.IsNullOrEmpty(fraction))
sb.Append(fraction.Trim() + " ");
if (!string.IsNullOrEmpty(direction))
sb.Append(direction.Trim() + " ");
if (!string.IsNullOrEmpty(street))
sb.Append(street + " ");
if (!string.IsNullOrEmpty(type))
if (sb.Length > 0)
sb.Append("<br />");
.....
return sb.ToString();
}
If this is MVC2/ASP.NET 4, you need to return an MvcHtmlString
instead of String
If not, then be sure you are not encoding the output with <%: %>
but instead use <%= %>
you're probably using the <%: %>
syntax which encodes the string.
Try using <%= %>
instead
or change your return type to MvcHtmlString
which will only require you to write return MvcHtmlString.Create(sb.ToString());
精彩评论