Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6003885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:11:04+00:00 2026-05-23T01:11:04+00:00

I’m putting together an MVC application where I have created a base Model which

  • 0

I’m putting together an MVC application where I have created a base Model which then has four derived Models, all of which inherit from the base Model:

public abstract class BaseFund
{
    public string Name { get; set; }
    public int AccountId { get; set; }
    public abstract decimal Value { get; }
    public virtual InvestmentAccount Account { get; set; }
}

one of the derived Models:

public class ShareFund : BaseFund
{
    public string ISIN { get; set; }
    public ShareFundType FundType { get; set; }
    public IncomeStatus IncomeStatus { get; set; }
    public decimal TotalShares {
        get
        {
            ICollection<ShareTransaction> tt = this.Transactions;
            var outgoings = Transactions.Count > 0 ? Transactions.Where(t => t.TransactionType.IsOutgoing.Equals(true)).Sum(a => a.Units) : 0;
            var incomings = Transactions.Count > 0 ? Transactions.Where(t => t.TransactionType.IsOutgoing.Equals(false)).Sum(a => a.Units) : 0;
            return incomings - outgoings;
        }
    }
    public override decimal Value
    {
        get
        {
            return this.TotalShares * (this.SharePrice / 100);
        }
    }
    public decimal SharePrice { get; set; }
    public ICollection<ShareTransaction> Transactions { get; set; }
}

And there are three other derived Models that are similar. All of the Models are POCO’s used by Entity Framework.

EDIT : The view is standard MVC scaffolding stuff at this stage:

<table>
<tr>
    <th>
        Name
    </th>
    <th>
        Account
    </th>
    <th>
        Value
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Account.AccountNumber)
    </td>
    <td>            
        @Html.DisplayFor(modelItem => item.Value)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
        @Html.ActionLink("Details", "Details", new { id=item.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.Id })
    </td>
</tr>
}
</table>

What I want to do is create a View that shows the fields from the Base Fund (Name, AccountId and Value). The problem is that the logic that works out the Value is different for every derived Model – in the case of ShareFund it uses TotalShares, so the BaseFund used in the View must be cast to be of type ShareFund. The other derived Models don’t necessarily have TotalShares as a property.

With that in mind:

  • Is using inheritance with Models in this way the way to go? If so, how do I get the fields specific to the derived models in the View?
  • If using inheritance is not recommended for this type of scenario, what should I use in its place?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:11:05+00:00Added an answer on May 23, 2026 at 1:11 am

    There turned out to be a simple answer to this. One of the Transaction properties further down the chain wasn’t being populated from the database by EF. This meant TransactionType was null which led to an null reference error in TotalShares. I misread this as meaning there was a problem with the property as it belonged to the derived Model and not the base Model.

    Thanks Lazarus, your comment led me to the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.