Casting model within Spark View

56 Views Asked by At

I have two objects that a model can be. I have a base class of ctrl1, with an inherited class of ctrl2. In my Spark view, I have a 'each' on a div to render out the controls in a list (these can be either ctrl1 or ctrl2). If I reference the base class properties in the each, it renders as expected. But if I try to reference a derived types properties, it throws an error saying ctrl1 does not contain a definition for the property I am trying to access. I have tried casting like so:

{(ctrl2)control.PropertyOnDerivedOnly}"

Where 'control' is the current item in the each loop. Is there any way I can cast to the object I am expecting using Spark syntax?

It's worth saying that although they can be either type within the each, I am doing an 'if' on this specific element, so I know that they will only be ctrl2 at this point. Hence the cast should be valid.

1

There are 1 best solutions below

0
Dave Thieben On

technically, you could set a temporary variable:

<var control2="control as ctrl2" />
<p>${control2.PropertyOnDerivedOnly}</p>

however, I would suggest you create a viewmodel that specifically serves this view, and fill the viewmodel from your controller, doing the casting at that point.