4

I am trying to do a ContainsKey check for a field in a razor view that is sometimes not present. Even when I have the ContainsKey if statement in my code I get the The given key was not present in the dictionary error. There are two fields (keys) that are not mandatory in the schemas but when the page loads and they are null it breaks. This is inside of a foreach loop. I am using VS to debug and when I set a breakpoint I can see the keys does not exist in the MetadataFields["userAgentCollection"].EmbeddedValues collections for some nodes. The code is posted below.

    @model DD4T.ContentModel.IPage

<span># Modified </span> @Model.MetadataFields["modifiedDate"].Value<br /><br />

@foreach(var userAgent in @Model.MetadataFields["userAgentCollection"].EmbeddedValues) 
{
    <span>User Agent:  </span>@userAgent["userAgent"].Value
    if (userAgent.ContainsKey("disallow"))
    {
        foreach (var disallow in @userAgent["disallow"].EmbeddedValues)
        {
            if (disallow["Disallow"].Value != "")
            {
                 <br />@disallow["Disallow"].Value
            }
        }
    }
 if (userAgent.ContainsKey("crawlDelay"))
        {
            <br /><span>Crawl Delay:  </span>@userAgent["crawlDelay"].Value
        }
<br /><br />
}

<span>Sitemap:  </span>@Model.MetadataFields["sitemap"].Value<br />
2
  • Oh and BTW Linklevel is set to 4
    – Rhonda
    Nov 27, 2013 at 22:19
  • Embedded Schema fields aren't affected by link level, only Component Links Nov 28, 2013 at 8:06

1 Answer 1

3

The error message is pointing to the error being in the dictionary types. I guess as you have already ensured that userAgent contains "userAgent", I would ask you to double check Model.MetadataFields["userAgentCollection"]. Ensure that userAgentCollection is in Model.MetadataFields.

@model DD4T.ContentModel.IPage

<span># Modified </span> @Model.MetadataFields["modifiedDate"].Value<br /><br />

if (@Model.MetadataFields.ContainsKey("userAgentCollection"))
{
  @foreach(var userAgent in @Model.MetadataFields["userAgentCollection"].EmbeddedValues) 
  {
    <span>User Agent:  </span>@userAgent["userAgent"].Value
    if (userAgent.ContainsKey("disallow"))
    {
        foreach (var disallow in @userAgent["disallow"].EmbeddedValues)
        {
            if (disallow["Disallow"].Value != "")
            {
                 <br />@disallow["Disallow"].Value
            }
        }
    }
 if (userAgent.ContainsKey("crawlDelay"))
        {
            <br /><span>Crawl Delay:  </span>@userAgent["crawlDelay"].Value
        }
    <br /><br />
  }
}
<span>Sitemap:  </span>@Model.MetadataFields["sitemap"].Value<br />
1
  • Turns out the code worked fine as is. It was just getting errors in Visual Studio but it works fine in Tridion. Once I posted my code back into the Tridion view it worked fine. Clearly our VS debugging tools has a few bugs. Rhonda
    – Rhonda
    Nov 29, 2013 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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