Note that there are some explanatory texts on larger screens.

plurals
  1. POBase Models and Derived Models in MVC
    text
    copied!<p>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:</p> <pre><code>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; } } </code></pre> <p>one of the derived Models:</p> <pre><code>public class ShareFund : BaseFund { public string ISIN { get; set; } public ShareFundType FundType { get; set; } public IncomeStatus IncomeStatus { get; set; } public decimal TotalShares { get { ICollection&lt;ShareTransaction&gt; tt = this.Transactions; var outgoings = Transactions.Count &gt; 0 ? Transactions.Where(t =&gt; t.TransactionType.IsOutgoing.Equals(true)).Sum(a =&gt; a.Units) : 0; var incomings = Transactions.Count &gt; 0 ? Transactions.Where(t =&gt; t.TransactionType.IsOutgoing.Equals(false)).Sum(a =&gt; a.Units) : 0; return incomings - outgoings; } } public override decimal Value { get { return this.TotalShares * (this.SharePrice / 100); } } public decimal SharePrice { get; set; } public ICollection&lt;ShareTransaction&gt; Transactions { get; set; } } </code></pre> <p>And there are three other derived Models that are similar. All of the Models are POCO's used by Entity Framework.</p> <p>EDIT : The view is standard MVC scaffolding stuff at this stage:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;th&gt; Name &lt;/th&gt; &lt;th&gt; Account &lt;/th&gt; &lt;th&gt; Value &lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Name) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Account.AccountNumber) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Value) &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | @Html.ActionLink("Details", "Details", new { id=item.Id }) | @Html.ActionLink("Delete", "Delete", new { id=item.Id }) &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; </code></pre> <p>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.</p> <p>With that in mind:</p> <ul> <li>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?</li> <li>If using inheritance is not recommended for this type of scenario, what should I use in its place?</li> </ul> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload