简体   繁体   English

在MVC 3中创建局部视图

[英]Create a partial view in MVC 3

I try to create Data Driven Menu in MVC 3. So i complete following code : 我尝试在MVC 3中创建“数据驱动菜单”。因此,我完成以下代码:

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 : 然后我尝试使用以下代码在_layout中将我的视图称为部分视图:

@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 在_layout页面

@Html.Action("Index","TreeMenu")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM