Location objects in Razor

The problem

Many record properties are represented by Location objects, for example the owner location.  This post examines the requirements for displaying them in a razor template.

Location as additional field

Most additional fields are simple values, such as strings or numbers.  The location type additional field is slightly more complex.  To display properties of a location you need the correct settings in hptrim.config as well as the correct razor.

Config

Use the defaultProperties config element to specify the location properties you wish to return always, whether they are requested in a particular route or not.  The example below will ensure that FormattedName and FullFormatted name are available. In fact that specifies that we want FormattedName and FullFormatted for every single location object.

  <defaultProperties>
    <clear />
    <add name="Location" properties="FormattedName,FullFormattedName" />
    ...
  </defaultProperties>

Razor

Location objects when used in additional fields use the reference version of the Location object, LocationRef.  This example demonstrates displaying the FormattedName.

LocationRef policeOfficer = trimObject.GetPropertyOrFieldValue("PoliceOfficer") as LocationRef;
if (policeOfficer != null) {
  <h1>@policeOfficer.FormattedName</h1>
}

Location as a property

Of course a location property will work in a similar fashion to the additional field.  First make sure the properties you require are set in defaultProperties then insert them as show here:

<h1>@trimObject.OwnerLocation.FormattedName</h1>

A pre-requisite

The above all assumes that you have pre-requested the additional field or property via the 'properties' request parameter.  For razor applications this is often done in the route, for example:

<add
  name="MyRoute"
  model="RecordFind"
  template="MyTemplate"
  properties="NameString,RecordOwnerLocation,PoliceOfficer"
/>
Written on June 8, 2015