0

I am relatively new to using ASP.NET MVC so forgive my ignorance if I am overlooking something simple here, but after a fair deal of research I have still found no solid solution.

I am passing a list of projects from my controller into my view of type: IEnumerable Project:

IEnumerable<Project> Projects = ViewBag.Projects;

Then looping through each project and populating a Datatable with the information. The only trouble is to get data for two of the columns in the Datatable I need to make a second DB query from within the loop.

I have created a @functions section to call a method in my repository which should return a list of archives related to any particular project:

View Top

@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);
    }
} 

View Main:

<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>

Method located within Repository:

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

Controller:

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

With this code as is I get a:

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

for return located in view:

return Archive_ElectronicRepository.GetListArchives(pid);

located in @functions, I know you more than likely shouldn't call the DB like this which is probably why I am getting the null exception error but I am at a loss here and can't really find a proper way of doing this.

1
  • Most of that view code belongs in the controller. Create a view model with the properties you want to display and populate in in the GET method. Commented Feb 25, 2015 at 11:17

1 Answer 1

2

Your private readonly IArchive_ElectronicRepository Archive_ElectronicRepository; doesn't seem to be instantiated or assigned which results in the NullReferenceException

Sign up to request clarification or add additional context in comments.

1 Comment

I definitely have been staring at a screen too long for over looking this one! My code now works but i'm concerned that I'm more than likely going about getting the data the wrong way. Seen as i'm getting it directly from the view. Any suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.