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.
Here is the _Menu.cshtml page, which is in the Shared folder:
@model IEnumerable<LangSite_151209.Models.MenuItem>
<div id="menu" class="largescreen_show smallscreen_hide" data-display="flex">
<div id="menu_left" class="menu_item">
@foreach (var mainMenuItem in Model)
{
// A bunch of stuff with the model that draws the menu
}
</div>
</div>
Here is how I call it in the _Layout page:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta and script stuff -->
</head>
<body style="overflow:hidden;">
<div id="fg">
<div id="mobile_wrapper">
@Html.Partial("../Shared/_Menu")
</div>
</div>
<!-- A bunch of footer stuff that's irrelevant here -->
</html>
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:
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<List<MenuItem>>(JsonString, settings);
return View(menuItems.ToList());
}
}
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.
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?