kailoon
kailoon

Reputation: 2069

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?

Upvotes: 9

Views: 4949

Answers (2)

LukeH
LukeH

Reputation: 269438

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

Upvotes: 12

Related Questions