简体   繁体   English

如何呈现将html标签保存为字符串值的列表字符串

[英]How to render a list string that hold html tags as string value

I have a list of alot of index that holds html tags like <input> , <select> and <td> . 我有很多索引列表,这些索引包含html标签,例如<input><select><td> My problem is that when rendering those values, it just prints its normal values not as html tags: 我的问题是,在渲染这些值时,它只是打印其正常值而不是html标签:

Img : 其中一部分呈现

  • Code: 码:

    @foreach(var field in ViewBag.Fields) { @foreach(ViewBag.Fields中的var字段){

      @field } 
  • I've tried this as well but both render the same: 我也尝试过,但是两者都呈现相同的效果:

     @foreach(var field in ViewBag.Fields) { @Server.HtmlEncode(field); } 
  • And

      @foreach(var field in ViewBag.Fields) { @Html.Encode(field); } 

But here the result: 但结果如下:

还是不行

What should I do to make it work? 我应该怎么做才能使其正常工作? Any suggestion? 有什么建议吗?

You are HTML encoding the values - this is why you are getting them output as encoded HTML (this is also the safe default of razor). 您正在对值进行HTML编码-这就是为什么要将它们输出为已编码的HTML(这也是剃刀的安全默认值)的原因。

Use the Raw helper to output HTML that is not encoded: 使用Raw助手来输出未编码的HTML:

@Html.Raw(field);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM