title: MVC 3 Dynamic View
body: <p>I am fairly new to MVC (3) and am building an app which will use lists of various data items throughout.</p>
  
  <p>I can build a view for each, but thought as each will be similar (HTML table with columns of data) it would make more sense to use a common view.</p>
  
  <p>The controllers all use Linq from a DAL to return entries eg</p>
  
  <p><em>DAL</em></p>
  
  <pre><code>public List &lt;MyObject&gt; getAllEntriesOfSomeType()
  {
      var myobjects = from r in db.something
                      where r.status ==1
                      select r;
      return myobjects.ToList();
  }
  </code></pre>
  
  <p><em>CONTROLLER</em></p>
  
  <pre><code>MyObjectClass o = new MyObjectClass();
  
  return View(o.getAllEntriesOfSomeType());
  </code></pre>
  
  <p><em>VIEW</em></p>
  
  <p>in the view, which would not be tied to this object, I would like to show the column names and data dynamically eg something like</p>
  
  <pre><code>&lt;thead&gt;
      &lt;tr&gt;
          @int cols = 0;
          @foreach(c in Model.Columns)
          {
              &lt;th&gt;
                  @cols+=1;
                  @c.DisplayName
              &lt;/th&gt;
          }
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
  
  @foreach(m in Model)
  {
      &lt;tr&gt;
          @(int i = 1;i&lt;=cols;i++)
          {
              &lt;td&gt;
                  @m[i].Value
              &lt;/td&gt;
          }
      &lt;/tr&gt;
  }
  &lt;/tbody&gt;
  </code></pre>
  
  <p>...</p>
  
  <p>Is there any (simple!) way of achieving the above. I thought of converting the model to DataTable but there didn't seem to be an easy way (read something about ObjectShredder)</p>