asp.net-mvc ASP.net MVC 4 从数据库加载菜单到局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14331766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.net MVC 4 loading menu from database to Partial View
提问by Haqi Petrucci
i'm an mvc newbie.
我是 mvc 新手。
What i'm trying to do is to load menu from database and display it in partial view. This partial view will be called from _layout view.
我想要做的是从数据库加载菜单并在部分视图中显示它。这个局部视图将从 _layout 视图中调用。
Home Controller
家庭控制器
in Home controller i add an action called "_MainMenu"
在 Home 控制器中,我添加了一个名为“_MainMenu”的操作
public class HomeController : Controller
{
mrpDatabase _db = new mrpDatabase();
public ActionResult _MainMenu()
{
return PartialView("_MainMenu", _db.menu.ToList());
}
....
}
PartialView
局部视图
this is my _MainMenu PartialView
这是我的 _MainMenu PartialView
@model IEnumerable<appMRP.Models.menu>
<ul id="menu">
@foreach (var item in Model)
{
<li>@item.menu1</li>
}
</ul>
Layout Page
布局页面
this partial menu is displayed in my _Layout.cshtml like this
这个部分菜单显示在我的 _Layout.cshtml 中
<nav>
@Html.Partial("_MainMenu")
</nav>
when i run this. i got error "NullReferenceException was unhandled by user code. Object reference not set to an instance of an object"
当我运行这个。我收到错误“用户代码未处理 NullReferenceException。未将对象引用设置为对象的实例”
seems the "Model" in my _MainMenu is null
似乎我的 _MainMenu 中的“模型”为空
what did i do wrong ?
我做错了什么 ?
thank you
谢谢你
回答by recursive
If your partial view uses a model, you need to pass it in like this:
如果您的局部视图使用模型,则需要像这样传递它:
@Html.Partial("_MainMenu", Model.ListOfMenus)
or something to that effect. Currently, you are not specifying a model for the _MainMenuview, so nullis used. When you attempt a @foreach, it throws the exception you see.
或类似的东西。目前,您没有为_MainMenu视图指定模型,因此null使用。当您尝试 a 时@foreach,它会抛出您看到的异常。
Note that @Html.Partial("_MainMenu")isn't calling your _MainMenucontroller action, just rendering the view of that name.
请注意,这@Html.Partial("_MainMenu")不是调用您的_MainMenu控制器操作,而是渲染该名称的视图。
If you want to call an action, you can use a child action like this:
如果你想调用一个动作,你可以使用这样的子动作:
@Html.Action("_MainMenu", "HomeController")
回答by Ravi Gadag
you already got the answer. check the _db.menu.ToList() is not returning null. I suggest you to use Model-View-ViewModelPattern. create model for view. and return it to View, so that you can control over the model and what to show to the view .
你已经得到了答案。检查 _db.menu.ToList() 没有返回 null。我建议你使用Model-View-ViewModel模式。创建视图模型。并将其返回到 View,以便您可以控制模型以及向视图显示的内容。
- You are not passing model information to the HTML Partial method, as the results, you try to iterate over the collection. so it will throw a null reference exception.
- 您没有将模型信息传递给 HTML Partial 方法,作为结果,您尝试迭代集合。所以它会抛出一个空引用异常。
some thing like this
像这样的事情
public class MenuViewModel
{
public int menuID { get; set; }
public string menuname { get; set; }
public string otherProperty { get; set; }
public string someotherProperty { get; set; }
}
in Your View
在你看来
@model IEnumerable<MenuViewModel>
<nav>
@Html.Partial("_MainMenu",Model)
</nav>

