Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an mvc razor @helper return a non-encoded tag?

This is contrived, but its basically what I'm doing. I have an a @helper like so:

@helper MyImage(int i) {
    String s = "<img src='{0}' />";
    String theImage = "";
    switch(i) {
        case 0: theImage = "test1.png"; break;
        case 1: theImage = "test2.png"; break;
        default: theImage = "error.png"; break;
    }
    String r = String.Format(s, theImage);
    @(r)
}

The output I get on the web page is of course the actual string:

<img src='test1.png' />

instead of the image. Is there a way to disable it from encoding it that way?

like image 756
kailoon Avatar asked Sep 01 '11 21:09

kailoon


2 Answers

You could use @Html.Raw(r) rather than just @(r).

like image 190
LukeH Avatar answered Oct 23 '22 23:10

LukeH


http://msdn.microsoft.com/en-us/library/system.web.mvc.mvchtmlstring.aspx

@(new MvcHtmlString(r))
like image 42
Valamas Avatar answered Oct 24 '22 00:10

Valamas