2

I have an abstract class with 3 other classes that inherit from this class:-

public abstract class AbstractTask
{
  [Key]
  public int ID {get; set;}
  [DisplayName("Featured Task")]
  public bool Featured { get; set; }
  // various other properties
}

public class HighLevelTask : AbstractTask
{
  [DisplayName("Workstream Description")]
  public String Workstream { get; set; }
  public virtual ICollection<MidLevelTask> MidLevelTasks { get; set; }
}

public class MediumLevelTask : AbstractTask
{
  public int HighLevelTaskID { get; set; }
  public virtual ICollection<DetailLevelTask> DetailLevelTasks { get; set; }
}

public class DetailLevelTask : AbstractTask
{
 public int MidLevelTaskID { get; set; }
}

So a High Level task can contain any number of Mid Level tasks and a Mid level task can contain any number of Detail tasks. Tasks at any level can be set as 'Featured' by the property inherited from the abstract class.

In a HTML view I want to present a nested list of the 'featured' tasks. So I am thinking in a Controller action something like this to collect all the featured tasks but am at a mental block on the best way to present this.

  var qryHighLevelTasks = from t in context.HighLevelTasks
                          where t.Featured == true
                          select t;
  var rsHighLevelTasks = qryHighLevelTasks.ToList();

  foreach(var highLevelTask in rsHighLevelTasks)
  {
     // get all mid level featured tasks related to this high level task
     var qryMidTasks = from midLevelTasks in context.MidLevelTasks
                       where midLevelTasks.Featured == true
                       && midLevelTasks.HighLevelTaskID == highLevelTask.ID
                       select midLevelTasks;
     var rsMidLevelTasks = qryMidTasks.ToList();

     foreach (var midLevelTask in rsMidLevelTasks)
     {
        // get all detail level featured tasks related to this mid level task
        var qryDetailTasks = from detailLevelTasks in context.DetailLevelTasks
                             where detailLevelTasks.Featured == true
                             && detailLevelTasks.MidLevelTaskID == midLevelTask.ID
                             select detailLevelTasks;
        var rsDetailLevelTasks = qryDetailTasks.ToList();                    
        }                
   }            

Perhaps I should have a composite model to represent the featured tasks? Or is there a better way? Any recommendations?

2
  • I would create a viewmodel, more code, but far more readable Commented Jun 13, 2012 at 7:21
  • It would be better to consider a 'Partial View' to render the nested list of the 'featured tasks'. In addition, try to focus on your view-model first. You may have a collection of 'featured tasks' in your view-model, and pass these collection to your partial view to be rendered. Commented Jun 13, 2012 at 11:56

2 Answers 2

2

I would suggest the 'Partial View' to render the nested list of the 'featured tasks'.

In addition, try to focus on your view-model first. You may have a collection of 'featured tasks' in your view-model, and pass these collection to your partial view to be rendered. Here you some links that might help:

1) Partial Views and Strongly Typed Custom ViewModels

2) How Do I: Work with Data in ASP.NET MVC Partial Views?

Sign up to request clarification or add additional context in comments.

1 Comment

@Gordon, any feedback on my answer ?
1

You can use LINQ projection to retrieve nested items:

var featuredItems = from hl in context.HighLevelTasks where hl.Featured == true
                    select new
{
   MidLevelTasks = from md in context.MidLevelTask where md.MidLevelTaskID = h1.TaskID,
   DetailLevelTasks = from ll in context.DetailLevelTask where ll.LowLevelTaskID = md.MidLevelTaskID
};

You can also use Editor Templates to render the nested Views:

Main view :

foreach(item in Model.HighLevelTasks)
{
   @Html.EditorFor(item);  
}

HighLevelTask View:

<!-- HTML here to render the parent record -->
foreach(item in Model.MidLevelTasks)
{
  @Html.EditorFor(item)
}

..in a similar pattern, create editor templates for Middle and Detail Level tasks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.