I have a razor component template which is looping through a list of component links trying to read out fields that come from an embedded schema. The Embedded Schema contains fields like Title, Body, Summary etc. I've read the Razor Mediator documentation which also indicates this is the correct method to adopt.

My code looks like this:

@foreach(dynamic item in List)

When I try to preview the page I get the typical razor error indicating the field doesn't exist:

Cannot perform runtime binding on a null reference

Can anyone see a problem here?


2 Answers 2


Ensure that item.EmbeddedSchemaName.EmbeddedSchemaFieldName is non-null for each item:

@foreach(dynamic item in List)
    if (item.EmbeddedSchemaName.EmbeddedSchemaFieldName != null)

It may be worth doing the same check for item.EmbeddedSchemaName.

From the Razor Mediator documentation:

The DynamicItemFields class will return null if no property name exists in the ItemFields, or if no value was input for single-valued fields.

This is in contrast to Dreamweaver templating, where you could expect an empty string in return for an empty Component field.

  • 1
    Thanks Ant, you're correct. I had 5 items in my list of component links, only 2 of which had content in the fields I was trying to render out. Feb 25, 2013 at 8:31

I have seen similar behaviour in other cms that use razor so I would agree to check for nulls unless you can use a Linq extension apply a where clause to your for loop

Shame you have to do a null check in a for loop but hey

  • I do not want to discourage you from answering questions here, because your participation on this site is appreciated, but your answer doesn't really seem to add much to Ant's previously provide answer. You might want to check out the following meta stack exchange on how to write a good answer to questions and take a shot at rewriting your answer: meta.stackexchange.com/questions/7656/… Feb 25, 2013 at 4:39
  • I think I did add to the solution by adding the suggestion of using Linq in the for condition Feb 26, 2013 at 5:45
  • I see where a LINQ could handle the null check: msdn.microsoft.com/en-us/library/bb882535.aspx. If using the Razor Mediator, do we need anything else to use LINQ expressions? I think all that's needed here is an example. Mar 2, 2013 at 6:45
  • Oh, and I also suspect the "tsk tsk" comment about needing to do a null check might look like criticism of either the product (which doesn't apply since this is an extension to SDL Tridion 2011) or the open source Razor Template Mediator itself. Its author, Alex Klock, would be interested in the perspective if he isn't already working on it. :-) Feel free to mention it on Google Code: code.google.com/p/razor-mediator-4-tridion. To be clear, this Razor isn't the same as Razor views on the content delivery side. Mar 2, 2013 at 6:48
  • I am sorry if you felt I was "tsking" but I was just attempting to improve the solution isnt that why we are here Mar 5, 2013 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.