0

I have the following class:

public class CourseSection
{
    [Key]
    public int CourseSectionID { get; set; }

    public int CourseID { get; set; }

    public string Title { get; set; }

    public virtual ICollection<SectionContent> SectionContents { get; set; }
}

And the child class SectionContent is as shown here:

public class SectionContent
{
    [Key, Column(Order = 0)]
    public int CourseSectionID { get; set; }
    [ForeignKey("CourseSectionID")]
    public virtual CourseSection CourseSection { get; set; }

    [Key, Column(Order = 1)]
    public int ContentID { get; set; }
    [ForeignKey("ContentID ")]
    public virtual Content Content { get; set; }

    public int ContentOrder { get; set; }
}

I want to be able to sort the list of sectioncontent based on the ContentOrder field, I have the following data in my sectioncontent table:

CourseSectionID         ContentID         ContentOrder
1                       212               1
1                       208               2
1                       214               3
1                       210               4

But when displaying this in the view I have been unable to order the section contents based on the ContentOrder property. It is being displayed based on ContentID so it is showing as 208,210,212,214. How can I order the SectionContents based on this property? This is my razor view code:

foreach (var sectionItem in Model.CourseSections)
{                     
  <li>
    <h5 class="accordion-title">@sectionItem.Title<span class="accordion-icon"></span></h5>
      <div class="accordion-content">
        <ul>                                    
        @foreach (var subSectionItem in sectionItem.SectionContents)
        {
          <li><a href="#" id="menuItem @subSectionItem.ContentID @sectionItem.CourseSectionID" onclick="SubItemMenu(id)">@subSectionItem.Content.Name</a></li> 
        }
        </ul>
      </div>
  </li>
}              
Jay
  • 3,012
  • 14
  • 48
  • 99

3 Answers3

2

Just add an OrderBy() statement to your foreach...

foreach (var sectionItem in Model.CourseSections)
{                     
  <li>
    <h5 class="accordion-title">@sectionItem.Title<span class="accordion-icon"></span></h5>
      <div class="accordion-content">
        <ul>                                    
        @foreach (var subSectionItem in sectionItem.SectionContents.OrderBy(sc => sc.ContentOrder))
        {
          <li><a href="#" id="menuItem @subSectionItem.ContentID @sectionItem.CourseSectionID" onclick="SubItemMenu(id)">@subSectionItem.Content.Name</a></li> 
        }
        </ul>
      </div>
  </li>
}

Alternatively you can do the OrderBy() when you fetch the data in the first place, and then assume that it's all in the right order in your view - the choice is yours.

theyetiman
  • 8,514
  • 2
  • 32
  • 41
1

The OrderBy extension method is what you're looking for.

@foreach (var subSectionItem in sectionItem.SectionContents.OrderBy(item => item.ContentOrder))
{
      <li><a href="#" id="menuItem @subSectionItem.ContentID @sectionItem.CourseSectionID" onclick="SubItemMenu(id)">@subSectionItem.Content.Name</a></li> 
}
twoflower
  • 6,788
  • 2
  • 33
  • 44
  • Cheers nice one I was trying this but with subSectionItem.ContentOrder which it wasnt recognising, item did the trick thanks again – Jay Mar 03 '14 at 13:55
  • 1
    @Jay just to add my 2 cents - it doesn't matter what you call `item`. you could call it `PaddingtonBear12345` if you wanted but it would still work. It's just a variable name for the instance of one `SectionContent` item. That's why, in my answer, I called it `sc` standing for `SectionContent` – theyetiman Mar 03 '14 at 14:01
  • I was trying subSection I guess was class with other variable just maybe – Jay Mar 04 '14 at 13:39
1

I imagine you have some sort of service method returning your CourseSection model. In that method, set the sorted model property:

var myCourseModel = buildMyCourseSection();
myCourseModel.SectionContents = (from sc in myCourseModel.SectionContents 
    order by sc.ContentOrder
    select sc).ToArray();
maf748
  • 768
  • 1
  • 4
  • 15
  • The previous answer helped me out just hadnt marked it but thanks for the input :) – Jay Mar 03 '14 at 13:55