MVC 3 Razor

I am really starting to like MVC 3 razor.   I’m continually reminded of old school ASP coding.  Even some situations where you wrote in <br> <hr> and other html tags into your business layer.

In the current application I’m working on I’m calling a system call and returning it as a string vs an object.   The reason is that the parameters are rendered then I send them back to the view.  To do this I first tried to do a line break “/r/n”.  The View and razor engine did not interpret this properly.

@myclass.PingIpAddress(“127.0.0.1”)

The “/r/n” was interpreted and did have a line break in the page but that was not shown as a return on the browser screen.

I then switched the string to contain an “<br //>” for a line break.

This returned &lt;br \&gt;  where the <br /> should have been

To correct the problem of always encoding the HTML you can use the Html.Raw call.  I’m surprised I have not ran into this issue before. (MSDN)

Here is the code @Html.Raw(myclass.PingIpAddress(“127.0.0.1”))

It shows the inline <br>.  Thanks to the google searches that helped me find how to get to MSDN to learn how to accomplish this.

 

 

1 thought on “MVC 3 Razor”

  1. You can also return MvcHtmlString.Create from your HTML helper to get past the encoding. Might work too. Just thought it was cleaner than putting the extra @Html.Raw in the View

Leave a Comment