Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 Simple Inheritance
    text
    copied!<p>I have come across a situation where the answer should be really straight forward, but it eludes me.</p> <pre><code>public class Note { #region Properties public int Id { get; set; } public int ClientId { get; set; } public int CandidateId { get; set; } public int TypeId { get; set; } public DateTime DateCreated { get; set; } public string UserId { get; set; } public string UserName { get; set; } public string Message { get; set; } #endregion #region Methods public void Save() { } #endregion } public class History : Note { } </code></pre> <p>As you can see, History inherits Note. They are exactly the same, the only difference between the two is the type Id.</p> <p>I have this function when getting data from the database</p> <pre><code> public static Note Parse(SqlDataReader dr) { int TypeId = Convert.ToInt32(dr["TypeId"]); Note Note; if (TypeId == 1) Note = new Note(); else Note = new History(); Note.Id = Convert.ToInt32(dr["Id"]); Note.TypeId = TypeId; if (dr["ClientId"] != DBNull.Value) Note.ClientId = Convert.ToInt32(dr["ClientId"]); if (dr["CandidateId"] != DBNull.Value) Note.CandidateId = Convert.ToInt32(dr["CandidateId"]); Note.DateCreated = Convert.ToDateTime(dr["DateCreated"]); Note.UserId = Convert.ToString(dr["UserId"]); Note.UserName = Convert.ToString(dr["UserName"]); Note.Message = Convert.ToString(dr["Message"]); return Note; } </code></pre> <p>And then on my MVC page I have this:</p> <pre><code>&lt;ol id="interview-comments"&gt; @foreach (Note Note in Model.Notes().OfType&lt;Note&gt;()) { } &lt;/ol&gt; &lt;ol id="history-comments"&gt; @foreach (History Note in Model.Notes().OfType&lt;History&gt;()) { } &lt;/ol&gt; </code></pre> <p>My question is simple. Is this the correct way to do it?</p> <p>/r3plica</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