我的代码中的 NullReferenceException

发布于 2024-10-06 23:48:35 字数 1420 浏览 0 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺渁∝ 2024-10-13 23:48:35

您没有将模型传递到局部视图。

@Html.Partial("_SideBarMenu")

所做的就是渲染一个名为 _SideBarMenu.cshtml 的部分视图

,但是您的部分视图需要: Cumavi.ViewModels.Sid​​ebarNavigation

传递模型:

@Html.Partial("_SideBarMenu", model)

另外 - 为什么你有一个 ctor对于SidebarNavigation,您在哪里填充类别?

您的控制器应该实例化该视图模型:

var model = new SideBarNavigational { Categories = repository.GetSomething() };
return View (model);

您永远不应该通过视图模型调用存储库。

You're not passing the model through to the partial view.

@Html.Partial("_SideBarMenu")

All that does is render a partial view called _SideBarMenu.cshtml

But your partial view expects: Cumavi.ViewModels.SidebarNavigation

Pass through the model:

@Html.Partial("_SideBarMenu", model)

Also - why do you have a ctor for SidebarNavigation where you populate the categories?

Your controller should instantiate that viewmodel:

var model = new SideBarNavigational { Categories = repository.GetSomething() };
return View (model);

You should never make calls to your repository via your view models.

合约呢 2024-10-13 23:48:35

删除您的私有访问器

public IEnumerable<Category> Categories { get; set; }

您确定 categoryRepo.FindAllCategories(); 返回不为 null 吗?

remove your private accessor

public IEnumerable<Category> Categories { get; set; }

Are you sure that categoryRepo.FindAllCategories(); returns not null?

苹果你个爱泡泡 2024-10-13 23:48:35

对于您的场景(如果我理解正确的话),您应该使用 Html.Action 而不是 Html.Partial 并创建一个用 ChildActionOnlyAttribute 装饰的新操作。正如对不同答案的评论中提到的,部分用于可重用标记。对于可重用的数据+标记,您可以使用子操作。

例如,您可以创建一个 SharedController 类:

public class SharedController : Controller
    [ChildActionOnly]
    public ViewResult SideBar() {
        return new PartialView(new SideBarModel());
    }
}

然后创建一个 SideBar.cshtml 视图:

@model SideBarModel
.... Render contents here

然后从您的 _Layout.cshtml 文件中调用:

@Html.Action("SideBar", "Shared")

For your scenario (if I understand it correctly) you should use Html.Action instead of Html.Partial and create a new action decorated with ChildActionOnlyAttribute. 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:

public class SharedController : Controller
    [ChildActionOnly]
    public ViewResult SideBar() {
        return new PartialView(new SideBarModel());
    }
}

And then have a SideBar.cshtml view:

@model SideBarModel
.... Render contents here

And then from your _Layout.cshtml file you call:

@Html.Action("SideBar", "Shared")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文