How to call properties of derived class in View?

Viewed 69

I have two models which derives from abstract class, becouse I need to store them in one list and order them by DateTime. Inheritance looks like this:

    public abstract class BaseActivity
    {
        public DateTime DateTime { get; set; }
    }
    public class Call : BaseActivity
    {
        //props//
    }
    public class Message : BaseActivity
    {
        //props//
    }

And now I'm using viewModel with this abstract class as property.

    public class ViewModel
    {
        //...other props...//
        public IEnumerable<BaseActivity> BaseActivities { get; set; }
    }

The thing is I should now display items from this list, but each type has own properties. Is there any other way to do this than switching by type in view like this?

@foreach (var activity in Model.BaseActivities )
{
    switch (activity)
    {
        case Call call:
                 @* display Call properties *@
            break;
        case Message message:
                 @* display Message properties *@
            break;
    }
}

0 Answers
Related