title: How can I run code from my layout file?
body: <p>I used the following tutorial to help me build an RSS Reader in my ASP.NET MVC3 Razor application:</p>
  
  <p><a href="http://weblogs.asp.net/jalpeshpvadgama/archive/2011/08/17/creating-basic-rss-reader-in-asp-net-mvc-3.aspx" rel="nofollow">http://weblogs.asp.net/jalpeshpvadgama/archive/2011/08/17/creating-basic-rss-reader-in-asp-net-mvc-3.aspx</a></p>
  
  <p>However, unlike the tutorial example, I want the RSS feed to be displayed on every page, and have therefore added it to my layout file, <code>/Views/Shared/_Layout.cshtml</code></p>
  
  <p>I currently only have 2 views on my site, and to get the RSS Reader to work on both views I've got the following code in my <code>HomeController</code>:</p>
  
  <pre><code>    public class HomeController : Controller
      {
          //
          // GET: /Index/
  
          public ActionResult Index()
          {
              return View(CT.Models.RssReader.GetRssFeed());
          }
  
          public ActionResult About()
          {
              return View(CT.Models.RssReader.GetRssFeed());
          }
  
      }
  </code></pre>
  
  <p>From my WebForms experience, I would simply add the RSS Reader code in my master page code behind, and it would automatically work on every page.</p>
  
  <p>Is there a Controller for layout pages which allows me to do the same?</p>
  
  <p>How can I get this to work on every call of the layout page, without having to return anything?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> Following @Sebastian's advice, I've now added this code to a Partial View, removed <code>CT.Models.RssReader.GetRssFeed()</code> from <code>return View()</code> and included this in my layout file:</p>
  
  <pre><code> @Html.Partial("_MyPartialView")
  </code></pre>
  
  <p>The code in this partial view is:</p>
  
  <pre><code>&lt;ul&gt;
      @foreach (var item in Model)
      {
        &lt;li&gt;
           &lt;a href="@item.Link" target="_blank"&gt;@item.Title&lt;/a&gt;
        &lt;/li&gt;
      }
  &lt;/ul&gt;
  </code></pre>
  
  <p>However, I'm not getting a runtime error:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>It's erroring on the line <code>@foreach (var item in Model)</code></p>