title: ASP.NET MVC models that order table data
body: <p>I have created an MVC project.  There is a default view and controller.  There is a model that has been expanded to cover two real tables and a view (db view).  That view and at least one of the tables need to be displayed AFTER sorting the data. I only see "simple" examples of creating models online; I'm not sure what words to search on. So, I have a class in its own file that defines the columns in a db-view</p>
  
  <pre><code>    using System.Data.Entity;
  
      namespace XYZ_dbo.Models {
          using System;
          using System.Collections.Generic;
          using System.ComponentModel.DataAnnotations;
          using System.ComponentModel.DataAnnotations.Schema;
          using System.Data.Entity.Spatial;
          using System.Linq;
          using System.Text;
  
          [Table("dbo.DockItemsByDept")]
          public partial class DockItemsByDept
          {
              public int Id { get; set; }
  
              [Required]
              [StringLength(128)]
              public string Name { get; set; }
              public int Dept { get; set; }
              public int Code { get; set; }
          }
      }
  </code></pre>
  
  <p>I have a model definition that references this class:</p>
  
  <pre><code>    using System.Data.Entity;
  
      namespace XYZ.Models
      {
          public partial class XYZModel : DbContext
          {
              public XYZModel()
                  : base("name=XYZModel")
              {
              }
  
              public virtual DbSet&lt;DockItemsByDept&gt; DockItemsByDept { get; set; }
  
              protected override void OnModelCreating(DbModelBuilder modelBuilder)
              {
                  modelBuilder.Entity&lt;DockItemsByDept&gt;()
                      .Property(e =&gt; e.Id);
                  modelBuilder.Entity&lt;DockItemsByDept&gt;()
                      .Property(e =&gt; e.Name)
                      .IsUnicode(false);
                  modelBuilder.Entity&lt;DockItemsByDept&gt;()
                      .Property(e =&gt; e.Dept);
                  modelBuilder.Entity&lt;DockItemsByDept&gt;()
                      .Property(e =&gt; e.Code);
              }
          }
      }
  </code></pre>
  
  <p>I can go into the <code>index.cshtml</code> view and say</p>
  
  <pre><code>    @model IEnumerable&lt;ITG_Calendar_Reservations.Models.DockItemsByDept&gt;
  
      &lt;div class="content"&gt;
          @foreach (var log in Model)
          {
              &lt;p&gt;@log.Name&lt;/p&gt;
          }
      &lt;/div&gt;
  </code></pre>
  
  <p>but I just get the default ordering the DB generates.  I want this "view" data sorted by the <code>Code</code> field then the <code>Name</code> field but I have no idea where such an operation or filter should be applied. I <em>thought</em> this would be in the model but I'm not seeing where it should go and I'm not finding anything useful when I search for an answer.</p>
  
  <p>Ideas?</p>