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