Leth Leth -3 years ago 148
Javascript Question

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.

This is how my iframe looks atm:

<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.

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

should be

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

The ' quote is used for character literals in c#

Now because you are using " quotes in razor you will need to change the html quotes to be '.

<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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download