【问题标题】:.Net Razor + Passing Raw HTML into @helper macro.Net Razor + 将原始 HTML 传递到 @helper 宏
【发布时间】:2013-06-27 13:33:08
【问题描述】:

我想将原始 html 传递给 @helper 宏...这可能吗?例如:

@helper oneColumn(string content) {
<div class="row">               
  <div class="twelve columns">
    @content                    
  </div>
</div>
}

@put.oneColumn(
  <h1 id="page-heading">HELLO WORLD!</h1>
)

【问题讨论】:

    标签: .net razor


    【解决方案1】:
    @helper oneColumn(Func<dynamic,HelperResult> content) {
    <div class="row">               
      <div class="twelve columns">
        @content(null)                    
      </div>
    </div>
    }
    
    @put.oneColumn( @<h1 id="page-heading">HELLO WORLD!</h1>)
    

    【讨论】:

    • 这对我来说有点魅力!应该是公认的答案。
    • 如果您是 razor 模板的新手,这应该有助于解释 @put.oneColumn Creating and Using a Helper in an ASP.NET Web Pages (Razor) Site
    • 该解决方案似乎只适用于 1 行。不确定如何将其用于多行 html。
    • 尝试原始提问者的调用方法,只要这些行都包含在标签或&lt;text&gt; iirc 中,该方法应该适用于多行。用我的方法签名风格
    【解决方案2】:

    使用@Html.Raw HTML 帮助程序,或将内容设为IHtmlString

    【讨论】:

    • @put.oneColumn(@Html.Raw(

      HELLO WORLD!

      ) ) 无效的表达式术语 '
    • @put.oneColumn(@IHtmlString(

      我的目标

      )) 无效的表达式术语'
    • @put.oneColumn((

      我的目标

      ).ToString()) 无效的表达式术语'
    【解决方案3】:
    @helper oneColumn(string content) {
    var contentHtml = new HtmlString(content);
    <div class="row">               
      <div class="twelve columns">
        @contentHtml                    
      </div>
    </div>
    }
    
    @put.oneColumn(
      "<h1 id=\"page-heading\">HELLO WORLD!</h1>"
    )
    

    【讨论】:

    • 问题是关于将原始 HTML 作为参数传递给帮助程序。您的参数是一个字符串,需要转义双引号和其他麻烦。
    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多