简体   繁体   English

将Razor数据插入src和href属性

[英]Getting Razor data inserted into src and href attributes

I need to insert an API key and a location string into the src attribute of my iframe element. 我需要在iframe元素的src属性中插入一个API密钥和一个位置字符串。

This is how my iframe looks atm: 这是我的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>

But when i go to the page, i get this error: 但是当我转到页面时,出现此错误:

Too many characters in character literal

I tried using "" instead for the razor properties, but that destroys the formatting for the rest of the dokument. 我尝试对剃刀属性使用“”代替,但这破坏了其余文档的格式。

I'm just really confused as to what is the correct way to use Razor syntax inside HTML attributes. 我真的很困惑在HTML属性中使用Razor语法的正确方法是什么。

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

should be 应该

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

The ' quote is used for character literals in c# '引号用于c#字符文字

Now because you are using " quotes in razor you will need to change the html quotes to be ' . 现在因为在剃须刀中使用"引号,您将需要将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>

它应该是@(Umbraco.Field("lokation")) -如果您的“变量”包含括号或其他特殊字符,例如“,当您已经在以“”分隔的文本字符串中时,则需要将整个内容括起来在...更多的寄生虫:-)

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

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