开发者

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

开发者 https://www.devze.com 2023-04-01 04:52 出处:网络
This is contrived, but its basically what I\'m doing. I have an a @helper like so: @helper MyImage(int i) {

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?


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


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

@(new MvcHtmlString(r))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号