Asp.net mvc4 Partial view with model in Layout
<p>I am trying to create the menus in my _Layout page using a partial view called _Menus which reads from a Json file on my server. But I can't figure out how to call a partial view with a model from the layout page. </p>

<p>Here is the _Menu.cshtml page, which is in the Shared folder:</p>

<pre><code>@model IEnumerable&lt;LangSite_151209.Models.MenuItem&gt;

&lt;div id="menu" class="largescreen_show smallscreen_hide" data-display="flex"&gt;
    &lt;div id="menu_left" class="menu_item"&gt;

        @foreach (var mainMenuItem in Model)
        {
           // A bunch of stuff with the model that draws the menu
        }
   &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>Here is how I call it in the _Layout page:</p>

<pre><code>&lt;!DOCTYPE html&gt;

&lt;html lang="en"&gt;
&lt;head&gt;
     &lt;!-- Meta and script stuff --&gt;
&lt;/head&gt;
&lt;body style="overflow:hidden;"&gt; 


    &lt;div id="fg"&gt;

        &lt;div id="mobile_wrapper"&gt;
        @Html.Partial("../Shared/_Menu")
        &lt;/div&gt;

    &lt;/div&gt;
    &lt;!-- A bunch of footer stuff that's irrelevant here --&gt;
&lt;/html&gt;
</code></pre>

<p>I have tried returning the partial view with a SharedController that opens the Json file and turns it into a model for the partial, as follows:</p>

<pre><code>public class SharedController : Controller
{
    // GET: Shared
    [ChildActionOnly]
    public ActionResult _Menu()
    {
        string filePath = HostingEnvironment.MapPath(@"~/App_Data/MenuItems.json");
        StreamReader sr = new StreamReader(filePath);

        string JsonString = sr.ReadToEnd();
        JsonSerializerSettings settings = new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.All
        };
        var menuItems = JsonConvert.DeserializeObject&lt;List&lt;MenuItem&gt;&gt;(JsonString, settings);

        return View(menuItems.ToList());
    }       
}
</code></pre>

<p>But when I try this I get a NullReferenceException on the Model: "Object reference not set to an instance of an object." Apparently you can't pass a model to a partial that way. I know that the code that reads the Json object works because when I use it the same code on a regular (non-partial) page it passes the model correctly and the script draws the menu.</p>

<p>Normally if I want to pass a model to a partial I just put the model in the main page and pass it that way. But I don't know how to put a model in the _Layout. Is that how I should to it? Or is there a better way?</p>