Reputation: 99
I am attempting to dynamically load a navigation menu from a database. I am using Entity Framework to pull the data. I have placed the menu in it's own partial view that gets loaded into the shared layout view. When I run the application, it fails before loading with the error, "An exception of type 'System.NullReferenceException' occurred in App_Web_h02ter44.dll but was not handled in user code.Additional information: Object reference not set to an instance of an object."
Model (NavigationMenuItem.cs)
using System;
using System.Collections.Generic;
public class NavigationMenuItem
{
public NavigationMenuItem()
{
this.NavigationMenuItems1 = new HashSet<NavigationMenuItem>();
}
public int MenuItemID { get; set; }
public Nullable<int> MenuItemParentID { get; set; }
public int MenuID { get; set; }
public string MenuItemName { get; set; }
public string MenuItemDescription { get; set; }
public string MenuItemText { get; set; }
}
Controller (NavigationMenuItemController.cs)
public class NavigationMenuItemController : Controller
{
private NorthwindEntityModel db = new NorthwindEntityModel();
// GET: /NavigationMenuItem/
public ActionResult Index()
{
var navigationmenuitems = db.NavigationMenuItems.Include(n => n.NavigationMenuItem1).Include(n => n.NavigationMenu);
return PartialView(navigationmenuitems.ToList());
}
}
_Layout.cshtml
<div class="col-md-4">
@Html.Partial("_NavigationBarPartial")
</div>
_NavigationBarPartial.cshtml
@model IEnumerable
@foreach (var item in Model) { @Html.DisplayFor(modelItem => item.MenuItemText) }UPDATE
Update: I created a folder inside the View folder named NavigationMenu Item with an Index.cshtml view inside. Same code as the _NavigationBarPartial.cshtml file i posted above. I then changed the _Layout div tag to this
<div class="col-md-4">
@Html.Action("Index", "NavigationMenuItem")
</div>
and now I'm pulling the menu items from the database as expected. Now I need to go back to using my original Shared/_NavigationBarBartial and make it work like the index file is now. I'm obviously missing something when it comes to calling the controller correctly from the shared partial view. I don't hit the break point in index action on my controller unless i'm using my new Index view to load it. I'm thinking it has to with whether i'm using Html.Partial, Html.Action, etc, and then the parameters i need to pass in to force it to call the correct controller, but I can't seem to figure that out. If anyone can spot the problem that would be great.
I really appreciate the help!!!
Upvotes: 0
Views: 1015
Reputation: 4607
Rather than having the @Html.Partial("_NavigationBarPartial")
in _Layouts.cshtml
just have a <div id="NavBarHere"></div>
Then, in a JavaScript file(assuming you have JQuery loaded)
$(document).ready(function(){
var options = {
url: "/NavigationMenuItem/Index",
type:"GET"}
$.ajax(options).done(function (data) {
var $target = $('#NavBarHere')
var $newHtml = $(data);
$target.replaceWith($newHtml);
});
});
This should replace the div you made with the markup in return by your Index (your partial view).
Upvotes: 0