【问题标题】:If not closed then Auto close or auto format html tag in dynamic content in razor如果未关闭,则自动关闭或自动格式化 razor 动态内容中的 html 标记
【发布时间】:2018-11-15 11:11:29
【问题描述】:

以下内容已从数据库中提取

<div class="main"><div class="col1">content</div>

在上面的示例中,我的数据库中的主 div 没有关闭,所以我想关闭它。

我只是在我的剃须刀视图页面中添加了

@Html.Raw(a.shortDesc)

但是我的页面被打扰了。所以请建议我。

【问题讨论】:

    标签: asp.net-mvc sql-server-2008 razor c#-4.0


    【解决方案1】:

    我建议使用 HtmlAgilityPack (https://html-agility-pack.net/) 修复 HTML,然后在视图中使用 @Html.Raw 渲染它。

    在 ViewModel 的 ShortDesc 属性中,您可以执行以下操作:

    private string _shortDesc;
    public string ShortDesc
    {
        get 
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(_shortDesc);
            return doc.DocumentNode.OuterHtml;
        }
        set 
        {
            this._shortDesc = value;
        }
    }
    

    【讨论】:

      【解决方案2】:
      @{
      var data = @a.shortDesc+"</div>"; 
      }
      @Html.Raw(@data)
      

      【讨论】:

      • 虽然这是对这段特定 HTML 的快速修复,但这可能会导致其他有效的 HTML 出现问题。
      • 将封闭的 div 字符串添加到数据库中的数据中
      猜你喜欢
      • 1970-01-01
      • 2010-10-18
      • 2015-09-30
      • 2018-05-17
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2018-03-05
      相关资源
      最近更新 更多