我的代码中的 NullReferenceException
我创建了一个 PartialView,下面是我的调用方式。
<div id="bodyarea">
<div id="leftnavigationbar">
@Html.Partial("_SideBarMenu")
</div>
<div id="mainbody">
@RenderBody()
</div>
<div id="footer">
</div>
</div>
下面是 PartialView 的实际代码:
@model Cumavi.ViewModels.SidebarNavigation
<ul>
@foreach (var category in Model.Categories)
{
<li>category.Name</li>
}
</ul>
如您所见,我使用的是自定义创建的名为 SidebarNavigation 的 ViewModel,其中包含以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cumavi.Models;
namespace Cumavi.ViewModels
{
public class SidebarNavigation
{
public IEnumerable<Category> Categories { get; private set; }
public SidebarNavigation()
{
CategoryRepository categoryRepo = new CategoryRepository();
this.Categories = categoryRepo.FindAllCategories();
}
}
}
问题是,当我运行应用程序时,我在 foreach 循环上收到空引用异常。
但我不明白原因。在 ViewModel、SidebarNavigation、构造函数中,我实际上正在填充变量。有什么建议吗?
编辑:
我注意到的另一件事是我的 ViewModel 类的构造函数从未被实际调用。 :S 这一定就是“类别”属性为空的原因。建议?
编辑2:
另一个问题!我正在使用 _Layout.cshtml 文件为应用程序创建通用外观(母版页)。由于没有控制器与该文件关联,我如何将模型传递给它? :S
I've created a PartialView and here's how I'm calling it.
<div id="bodyarea">
<div id="leftnavigationbar">
@Html.Partial("_SideBarMenu")
</div>
<div id="mainbody">
@RenderBody()
</div>
<div id="footer">
</div>
</div>
Here's the actual code for the PartialView:
@model Cumavi.ViewModels.SidebarNavigation
<ul>
@foreach (var category in Model.Categories)
{
<li>category.Name</li>
}
</ul>
As you can see, I'm using a custom created ViewModel called SidebarNavigation, which has this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cumavi.Models;
namespace Cumavi.ViewModels
{
public class SidebarNavigation
{
public IEnumerable<Category> Categories { get; private set; }
public SidebarNavigation()
{
CategoryRepository categoryRepo = new CategoryRepository();
this.Categories = categoryRepo.FindAllCategories();
}
}
}
The problem is that when I run the application, I get a null reference exception on the foreach loop.
I don't understand the reason though. In the ViewModel, SidebarNavigation, in the constructor I'm actually filling the variable. Any suggestions?
Edit:
Another thing I've noticed is that the constructor for my ViewModel class is never actually called. :S That must be why the Categories attribute is null. Suggestions?
Edit 2:
Another problem! I'm using _Layout.cshtml file to create the common look (masterpage) for the application. Since no controller is associated to this file, how can I pass a model to it? :S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有将模型传递到局部视图。
所做的就是渲染一个名为 _SideBarMenu.cshtml 的部分视图
,但是您的部分视图需要:
Cumavi.ViewModels.SidebarNavigation
传递模型:
另外 - 为什么你有一个 ctor对于SidebarNavigation,您在哪里填充类别?
您的控制器应该实例化该视图模型:
您永远不应该通过视图模型调用存储库。
You're not passing the model through to the partial view.
All that does is render a partial view called _SideBarMenu.cshtml
But your partial view expects:
Cumavi.ViewModels.SidebarNavigation
Pass through the model:
Also - why do you have a ctor for SidebarNavigation where you populate the categories?
Your controller should instantiate that viewmodel:
You should never make calls to your repository via your view models.
删除您的私有访问器
您确定 categoryRepo.FindAllCategories(); 返回不为 null 吗?
remove your private accessor
Are you sure that categoryRepo.FindAllCategories(); returns not null?
对于您的场景(如果我理解正确的话),您应该使用
Html.Action
而不是Html.Partial
并创建一个用ChildActionOnlyAttribute
装饰的新操作。正如对不同答案的评论中提到的,部分用于可重用标记。对于可重用的数据+标记,您可以使用子操作。例如,您可以创建一个
SharedController
类:然后创建一个
SideBar.cshtml
视图:然后从您的
_Layout.cshtml
文件中调用:For your scenario (if I understand it correctly) you should use
Html.Action
instead ofHtml.Partial
and create a new action decorated withChildActionOnlyAttribute
. As mentioned in a comment to a different answer, partials are meant for reusable markup. For reusable data+markup you can use child actions.For example you can create a
SharedController
class:And then have a
SideBar.cshtml
view:And then from your
_Layout.cshtml
file you call: