1

I'm really new to C#, but I need some guidance. I have an MVC site that uses a connection to a database (via entity framework) to keep track of projects and the tasks involved in each one. The first screen brings up the list of projects and all the information for them just fine, but when I click a link to bring up the list of project tasks, it kicks up a NullreferenceException error and I can't figure out how to fix it. I understand the concept of the error and why, I just don't see how it applied to this particular error.

Here is my Index.cshtml:

@{
      ViewBag.Title = "Tasks of Project 1";
}

@model IEnumerable<IntelliBase.Models.Task>

@{  Layout = null; }

...
...
...

<!--Task Grid-->
<div id="task-grid" class="table">
    <div class="table-header">         
        <span class="thCell ">ID</span>
        <span class="thCell ">Due Date</span>
        <span class="thCell ">Department</span>
        <span class="thCell ">Task Description</span>
        <span class="thCell ">Activity</span>

    </div>
    <div class="table-body">
        @foreach (var taskitem in Model)
        {
            <div class="row rowzibra">
                <span class="cell"><a href="../home/"><span class="drill-icon ui-icon ui-icon-zoomin"></span></a></span>
                <span class="cell"><a href="../home/">@Html.DisplayFor(modelItem => taskitem.TaskDueDate)</a></span>
                <span class="cell">@Html.DisplayFor(modelItem => taskitem.TaskDescription)</span>
                <span class="cell activity-link ">
                    <a href="javascript:editDialog()"><span class="edit-icon ui-icon ui-icon-pencil "></span>Edit</a>&nbsp;
                    <a href="javascript:deleteDialog('@Html.DisplayFor(modelItem => taskitem.TaskID)')"><span class="delete-icon ui-icon ui-icon-trash"></span>Delete</a>
                    <a href="javascript:detailsDialog()"><span class="drill-icon ui-icon ui-icon-note"></span>Details</a>
                </span>
            </div>
        }
        <div class="table-footer">
            <span>&nbsp;</span>
        </div>
    </div>
</div>

The error occurs on "Model" in the @foreach (var taskitem in Model) line.

The Model: TaskPartial.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IntelliBase.Models
{
    public partial class Task
    {

    }
}

The Controller: HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Diagnostics;

namespace IntelliBase.Controllers
{
    public class HomeController : Controller
    {

        Models.IntelliBaseEntities db = new Models.IntelliBaseEntities();

    }
}

The cshtml page for the Projects uses the same methodology to display the project items from the entity framework and seems to work just fine, but trying to use the above code to display the tasks for a project won't. I have no idea why "Model" in the projects page works fine, and "Model" in the Tasks page kicks up the NullReferenceException error. I would greatly appreciate any help! Thanks in advance!

@mfanto: here is the full controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Diagnostics;

namespace IntelliBase.Controllers
{
    public class HomeController : Controller
    {

        Models.IntelliBaseEntities db = new Models.IntelliBaseEntities();

        public ActionResult EFtest()
        {

            IntelliBase.Models.IntelliBaseEntities db = new Models.IntelliBaseEntities();          

            return View();
        }
        public ActionResult Test()
        {
            // sample for network folder access 
            // via windows explorer

            string path = @"\\SERVER2011";
            Process.Start("explorer.exe",path);


            return View();
        }
        public ActionResult Index()
        {
            ViewBag.Title = "Tasks";            
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }


        public ActionResult Search()
        {
            ViewBag.Title = "Search Tasks";

            return View();
        }
        public ActionResult SearchResults()
        {
            ViewBag.Title = "Search Results";
            return View();
        }
    }
}

1 Answers1

2

If Model is null, then it means you're not setting it inside your controllers action. Your code should look something like this:

public ActionResult EFtest()
{
    IntelliBase.Models.IntelliBaseEntities db = new Models.IntelliBaseEntities();          

    var items = db.Tasks.ToList();

    return View(items);
}

This will set Model equal to the list of items

mfanto
  • 14,168
  • 6
  • 51
  • 61