I try to create Data Driven Menu in MVC 3. So i complete following code :
Model :
#region MenuTree
public class MenuTree : BusinessObject
{
#region Constructor
public MenuTree()
{
}
#endregion
#region Property
#region ParentID
private int _nParentID;
public int ParentID
{
get { return _nParentID; }
set { _nParentID = value; }
}
#endregion
#region MenuName
private string _sMenuName;
public string MenuName
{
get { return _sMenuName; }
set { _sMenuName = value; }
}
#endregion
#region LinkText
private string _sLinkText;
public string LinkText
{
get { return _sLinkText; }
set { _sLinkText = value; }
}
#endregion
#region ActionName
private string _sActionName;
public string ActionName
{
get { return _sActionName; }
set { _sActionName = value; }
}
#endregion
#region ControllerName
private string _sControllerName;
public string ControllerName
{
get { return _sControllerName; }
set { _sControllerName = value; }
}
#endregion
#endregion
#region Functions
public MenuTree Get(int MenuTreeID)
{
return MenuTreeService.Instance.Get(new ID(MenuTreeID));
}
public ID Save()
{
return MenuTreeService.Instance.Save(this);
}
public void Delete()
{
MenuTreeService.Instance.Delete(ID);
}
#endregion
}
Controller Part :
public class TreeMenuController : Controller
{
//
// GET: /TreeMenu/
public ActionResult Index()
{
return View(MenuTrees.Gets());
}
}
view :
@model ESimSolMVC05.Models.MenuTrees
@{
ViewBag.Title = "Index";
}
<table>
<thead>
<tr>
<th> ID </th>
<th> ParentID</th>
<th> Menu Name</th>
</tr>
</thead>
<tbody>
@foreach (ESimSolMVC05.Models.MenuTree item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.ObjectID)</td>
<td>@Html.DisplayFor(modelItem => item.ParentID)</td>
<td>@Html.DisplayFor(modelItem => item.MenuName)</td>
</tr>
}
</tbody>
</table>
then I try to call my view as a partial view in _layout with following code :
@Html.Partial("~/Views/TreeMenu/index.cshtml")
But When i run my project I get an exception
My Exception Message is : Object reference not set to an instance of an object.
Any one suggest me I can I call a partial view
public PartialViewResult Index()
{
return PartialView(MenuTrees.Gets());
}
use:
@Html.Partial("Index","TreeMenu")
Change Action to
public ActionResult Index()
{
return PartialView(MenuTrees.Gets());
}
In _layout page
@Html.Action("Index","TreeMenu")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.