从视图中的循环调用查询方法

时间:2015-02-25 11:10:00

标签: c# asp.net-mvc asp.net-mvc-4

我对使用ASP.NET MVC相对较新,所以如果我在这里忽略了一些简单的东西,请原谅我的无知,但经过大量的研究,我仍然没有找到可靠的解决方案。

我将控制器中的项目列表传递给我的类型视图:IEnumerable Project:

IEnumerable<Project> Projects = ViewBag.Projects;

然后循环遍历每个项目并使用信息填充Datatable。唯一的麻烦是获取数据表中我需要从循环内进行第二次数据库查询的两个列的数据。

我创建了一个@functions部分来调用我的存储库中的方法,该方法应返回与任何特定项目相关的存档列表:

查看顶部

@using stwintranet.Areas.PQP.Models
@model Year

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    IEnumerable<Project> Projects = ViewBag.Projects;
    IEnumerable<Year> Years = ViewBag.Years;
    String year = ViewBag.URLYear;
} 
@functions
{
    private readonly IArchive_ElectronicRepository Archive_ElectronicRepository;

    public IEnumerable<Archive_Electronic> GetListArchives(int pid)
    {
        return Archive_ElectronicRepository.GetListArchives(pid);
    }
} 

查看主要内容:

<table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
    <thead>
        <tr>
            <th>Project Year</th>
            <th>Project Number</th>
            <th>Project Title</th>
            <th>Client</th>
            <th>Site Address</th>
            <th>Director</th>
            <th>Archive Status</th>
            <th>Primary Location</th>
            <th>Location</th> <!-- Requires 2nd Query -->
            <th>Documents</th> <!-- Requires 2nd Query -->
        </tr>
    </thead>
    <tbody>
        @foreach (var project in Projects)
        {
            //Get a list of archived projects
            IEnumerable<Archive_Electronic> ArchiveProjects = GetListArchives(project.ProjectID);
            string Docs = "";
            string Locs = "";
            foreach (var archive in ArchiveProjects)
            {
                //Write List of Docs
                if (archive.DocumentType == 1)
                {
                    Docs = Docs + "Dwg ";
                }
                else if (archive.DocumentType == 2)
                {
                    Docs = Docs + "Doc ";
                }
                else if (archive.DocumentType == 3)
                {
                    Docs = Docs + "Eml ";
                }
                //Write List of Locations
                if (archive.Location == 2)
                {
                    Locs = Locs + "Dub ";
                }
                else if (archive.Location == 3)
                {
                    Locs = Locs + "Gal ";
                }
                else if (archive.Location == 4)
                {
                    Locs = Locs + "Lon ";
                }
                else if (archive.Location == 6)
                {
                    Locs = Locs + "Cor ";
                }
            }


            <tr id="@project.ProjectNumber" onclick="showProject(this.id)">
                <td>@project.Year</td>
                <td>@project.ProjectNumber</td>
                <td>@project.ProjectTitle</td>                                  
                <td>@project.Organisation.Organisation1</td>
                <td>@project.SiteAddress1</td>
                <td>@project.ProjectDirector</td>
                <td>Archive Status will go here</td>
                <td>@project.SiteArea</td>
                <td>@Locs</td>
                <td>@Docs</td>
            </tr>

        }


    </tbody>
</table>

位于存储库中的方法:

public IEnumerable<Archive_Electronic> GetListArchives(int id)
{
    return context.Archive_Electronic.Where(x => x.Project == id).DefaultIfEmpty();
}

控制器:

// GET: PQP/Stage9/ElectronicArchiveReport
public ActionResult ElectronicArchiveReport(string id)
{
    ViewBag.Years = YearRepository.GetYears();
    ViewBag.Projects = ProjectRepository.GetListProjects(id);
    ViewBag.URLYear = id;
    return View();
}

使用此代码,我得到了:

System.NullReferenceException: Object reference not set to an instance of an object.

返回位于视图中:

return Archive_ElectronicRepository.GetListArchives(pid);

位于@functions,我知道你很可能不应该像这样调用DB这可能是我得到null异常错误的原因但是我在这里不知所措并且无法找到正确的方法这样做。

1 个答案:

答案 0 :(得分:2)

您的private readonly IArchive_ElectronicRepository Archive_ElectronicRepository;似乎没有被实例化或分配,导致NullReferenceException