【问题标题】:Getting Razor data inserted into src and href attributes将 Razor 数据插入 src 和 href 属性
【发布时间】:2017-08-17 11:49:21
【问题描述】:

我需要在 iframe 元素的 src 属性中插入一个 API 密钥和一个位置字符串。

这就是我的 iframe 的外观:

<iframe class="center-block" src="https://www.google.com/maps/embed/v1/place?key=@Umbraco.Field('aPIKey')&q=@Umbraco.Field('lokation')" width="95%" height="300" frameborder="0" style="border:0" allowfullscreen></iframe>

但是当我转到页面时,我得到了这个错误:

Too many characters in character literal

我尝试使用“”代替剃刀属性,但这会破坏文档其余部分的格式。

我真的很困惑在 HTML 属性中使用 Razor 语法的正确方法是什么。

【问题讨论】:

    标签: javascript asp.net-mvc iframe razor umbraco


    【解决方案1】:
    @Umbraco.Field('lokation') @Umbraco.Field('aPIKey')
    

    应该是

    @Umbraco.Field("lokation") @Umbraco.Field("aPIKey")
    

    ' 引号用于c# 中的字符文字

    现在因为您在剃须刀中使用" 引号,您需要将html 引号更改为'

    <iframe class="center-block" src='https://www.google.com/maps/embed/v1/place?key=@Umbraco.Field("aPIKey")&q=@Umbraco.Field("lokation")' width="95%" height="300" frameborder="0" style="border:0" allowfullscreen></iframe>
    

    【讨论】:

    • 这对我不起作用,因为它的格式不正确。 i.imgur.com/5oDCDK2.png
    • 太好了,如果您能将其标记为正确,我将不胜感激。谢谢。
    【解决方案2】:

    它应该是@(Umbraco.Field("lokation")) - 如果你的“变量”包含括号或其他特殊字符,例如“当你已经在一个“”分隔的文本字符串中时,你需要把整个东西包起来……更多括号:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多