11

My component has fields like (text,image,componentlinks). I have faced two issues

  1. since, here we cannot use <a tridion:href="@fields.internal_link"></a>, so how can we create a component link using DD4T template?
  2. If some fields left blank it shows an error msg "The given key was not present in the dictionary"

Normally in razor it works fine,but with DD4T its showing me the error

@if(@model.component.fields["quote_text"] != null){
    <p>@model.component.fields["quote_text"].Value</p>
}

Did I miss anything or we have some different approach for DD4T?

11

This works differently in DD4T. Unlike when dealing with the dynamic ComponentModel.Fields exposed by the Razor Mediator, you're now working with a standard dictionary, so you need to check that the key exists before you can access it. Try this:

@if (Model.Component.Fields.ContainsKey("quote_text"))
{
    <p>@Model.Component.Fields["quote_text"].Value</p>
}

As for your question regarding component linking, you can resolve a Component link using the LinkFactory.ResolveLink method.

LinkFactory lf = new LinkFactory();
var href = lf.ResolveLink(someTcmId);
  • I tried this ,its working. – roop Jun 24 '13 at 9:17
  • If the information helped you, you may want to mark it as the accepted answer. – Rob Stevenson-Leggett Jun 25 '13 at 12:38
8

Shameless plug but check out the BuildingBlocks.DD4T.MarkupModels project, it takes care of a lot of this for you. It is just an alpha at the moment though.

Alternatively you could look at creating a suite of Html Helpers to do the job of checking for null and resolving Component Links etc.

5

To answer your first question: you don't have to do anything other than use standard DD4T TBBs which generate the XML to create component links. On the Delivery side, on a component link field you use the method .ResolveUrl().

Regarding your second question: As mentioned by others, use .ContainsKey() to determine if a field exists. If you have a complicated schema, then you may want to create a ViewModel class which takes your model as the input and does all the validations in its constructor.

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.