Below is DD4T code that I need to rewrite to DXA. Find Schema name of the Linked Component and perform an action based on the schema.

    IComponent component = Model.Fields["linkedComponent"].LinkedComponentValues[0];
    if (component.Schema.Title == "SomeSchema")
    {
        if (component.Fields.ContainsKey("field1"))
        {
            string form = component.Fields["field1"].Value;
        }
        if (component.Fields.ContainsKey("field2"))
        {
            string formID = component.Fields["field2"].LinkedComponentValues[0].Fields["SecondLevelField"].Value;
        }
    }
    else if(component.Schema.Title == "SomeOtherSchema")
    {
        if (component.Fields.ContainsKey("someOtherField"))
        {
            string someOtherField = component.Fields["someOtherField"].Value;
        }
    }

Is this possible in DXA2.0

  • 2
    Why don’t you try it and share your findings with us – Nickoli Roussakov Jul 10 at 12:24

The DXA way of doing such thing is using Strongly Typed View Models and Semantic Mapping.

Using a smart combination of these two, you might not need additional logic at all.

In case you still need Schema-specific custom logic, you can test the type of the View Model (which maps to a CM Schema).

The given code fragment is too small/isolated to give an proper example of Strongly Typed View Models.

You could define View Model Types SomeSchema and SomeOtherSchema, which have a common base class. The propery LinkedComponents can then be of that base class (in general, these names are not ideal, because referring to CM Concepts; you should be using more semantic names for your View Model Types and Properties). This makes the LinkedComponent property polymorphic: the property is of a given type, but the actual linked entities will always be subclasses of that type (and you can test which subclass if needed).

Alternatively, you could define a Strongly Typed View Model which can represent the information from both Schemas and maps to both Schemas.

  • Thanks Rick.. Am yet to try this. soon will post approach that worked for me.. – Balajhi SV Jul 18 at 10:27

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.