6

I am using the below code to access the field values for a linked component.

In the foreach line, it throws error that

The given key was not present in the dictionary

I have checked the component and the "main_nav" field has a component.

I have also tried by increasing the linklevel parameter but no luck. Please advise.

Added more details of the code.

   @foreach (var link in Model.Component.Fields["main_nav"].LinkedComponentValues) 
   { 
      <li id="@link.Fields["title"].Value.ToLower()">
      if (!link.Fields.ContainsKey("internal_link") && !link.Fields.ContainsKey("external_link")) { 
           <a id="@link.Fields["title"].Value.ToLower()Prompt">@link.Fields["title"].Value</a>
       } 
   }

And here is the stack trace returned:

[KeyNotFoundException: The given key was not present in the dictionary.] System.Collections.Generic.Dictionary`2.get_Item(TKey key) +9624813 ASP._Page_Views_Shared__Header_cshtml.Execute() in File location..34 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +208 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +97 System.Web.WebPages.StartPage.RunPage() +19 System.Web.WebPages.StartPage.ExecutePageHierarchy() +67 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303

6
  • Are you positive the error is thrown in the lines that you quote? It doesn't seem possible, because you are checking if the key exists before you read it from the dictionary!
    – Quirijn
    Commented Jul 2, 2013 at 20:49
  • Can you share the code that's inside the foreach loop? Commented Jul 3, 2013 at 5:56
  • Perhaps also write out the type of main_nav just to confirm this is a linked component and that it can be read. Commented Jul 3, 2013 at 6:34
  • @foreach (var link in Model.Component.Fields["main_nav"].LinkedComponentValues) { <li id="@link.Fields["title"].Value.ToLower()"> if (!link.Fields.ContainsKey("internal_link") && !link.Fields.ContainsKey("external_link")) { <a id="@link.Fields["title"].Value.ToLower()Prompt">@link.Fields["title"].Value</a> } if (link.Fields.ContainsKey("internal_link")) { if(link.Fields["type"].Value == "Open New Window") { <a id="@link.Fields["title"].Value.ToLower()Prompt" href="@link.Fields["internal_link"].LinkedComponentValues[0].Id" target="_blank">@link.Fields["title"]...
    – roop
    Commented Jul 3, 2013 at 8:23
  • 1
    You're not checking if the fields 'type' and 'title' on the linked component exist. Could it be that one of those fields is missing from the content?
    – Quirijn
    Commented Jul 3, 2013 at 13:30

2 Answers 2

5

The most likely explanation is that the main_nav field is not of the type Component Link. It might be a text field or some other field.

For all field types except Component Link and Multimedia Link, the LinkedComponentValues property is null.

1
  • I checked again as you suggested,its of type Component Link.Is there any other way to fix this.
    – roop
    Commented Jul 2, 2013 at 12:23
2

Are you sure you republished the page after changing the link level on your Component Template?

You should check your Content Data Store (Broker) database PAGE_CONTENT table for the XML and check that the main_nav field LinkedComponentValues element is populated with what you expect.

1
  • I've just experienced the same issue but solved it. Be sure to set the proper Link Level and make sure that the updated link level is propagated. I did that by closing the Template Builder, refreshing the browserm unpublishing and republishing the item and by checking the XML in the broker database.
    – Raimond
    Commented Jul 8, 2013 at 21:47

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.