title: MVC Partial View with List
body: <p><strong>Answered</strong></p>
  
  <p><strong>Dbaseman was correct</strong> but to simplify it all, I don’t need a modal and my code looks like this now:</p>
  
  <pre><code>[ChildActionOnly]
  public ActionResult RecentNews()
  {
      return PartialView(db.Articles.ToList());
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@{Html.RenderAction("RecentNews");}
  </code></pre>
  
  <p><strong>Below is my old code that wasn’t working</strong></p>
  
  <p>I’m having trouble getting the following to work, I’m unsure if I’m even close to being on the right track or not. The code all works fine if I create a View but not if I create a partial View and add it to other views. Whatever I try I seem to get  the error : Object reference not set to an instance of an object.</p>
  
  <p>In my view I have:</p>
  
  <pre><code>@Html.Partial("RecentNews")
  </code></pre>
  
  <p>My action looks like this:</p>
  
  <pre><code>public ActionResult RecentNews()
  {
      var rn = (from m in db.Articles
                  select new RecentNews
                  {
                      ArticleHeading = m.ArticleHeading
                  });
      return View(rn);
  
      //return View(db.Articles.ToList());
  }
  </code></pre>
  
  <p>My modal:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace melbournesportsstadium.Models
  {
      public class RecentNews
      {
          public string ArticleHeading { get; set; }
      }
  }
  </code></pre>
  
  <p>And my RecentNews.cshtml:</p>
  
  <pre><code>@model List&lt;melbournesportsstadium.Models.RecentNews&gt;
  
  &lt;table&gt;
  
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.ArticleHeading)
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  
  &lt;/table&gt;
  </code></pre>
  
  <p>Can someone please help me get this working?</p>