4

In ComponentController.cs in the DD4t.Web.mvc project I am getting the following error:

KeyNotFoundException was unhandled by usercode

The given key was not present in the dictionary.

The Exception occurs when I get to the line:

var model = ViewModelBuilder.Build(componentPresentation);

1 Answer 1

3

The exception is probably just a simple exception telling you that the Build method is trying to read a field value of a field in a Component's IFieldSetdictionary for a dictionary key that is defined in your code, but not set in the published component.

Edit the Build method to always check whether a field actually consists in the published component:

if (tridionComponent.Fields.ContainsKey("title"))
{
    Title = tridionComponent.Fields["title"].Value;
}// else return an empty string, or throw the exception if the field is really always needed

It always helps to ensure that values are set properly in Tridion Components by setting the field in the Component's Schema to Mandatory. In any case, I personally always find it better to log an error stating that the field isn't present rather than to kill the whole page, therefore I always program my view models as defensively as possible.

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.