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

  • 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 Jul 2 '13 at 20:49
  • Can you share the code that's inside the foreach loop? – Albert Romkes Jul 3 '13 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. – Dylan .. Mark Saunders Jul 3 '13 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 Jul 3 '13 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 Jul 3 '13 at 13:30
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.

| improve this answer | |
  • I checked again as you suggested,its of type Component Link.Is there any other way to fix this. – roop Jul 2 '13 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.

| improve this answer | |
  • 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 Jul 8 '13 at 21:47

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.