NullReferenceException in MVC

本文关键字:MVC in NullReferenceException | 更新日期: 2023-09-27 18:13:10

我对c#真的很陌生,但我需要一些指导。我有一个MVC网站,使用连接到数据库(通过实体框架)来跟踪项目和每个项目所涉及的任务。第一个屏幕显示项目列表和它们的所有信息,但是当我单击一个链接以显示项目任务列表时,它会弹出一个NullreferenceException错误,我不知道如何修复它。我理解误差的概念和原因,我只是不明白它如何应用于这个特定的误差。

这是我的索引。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>

错误发生在@foreach (var taskitem in Model)行的"Model"上。

模型:taskpart .cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace IntelliBase.Models
{
    public partial class Task
    {
    }
}

控制器:homeconcontroller .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();
    }
}

项目的cshtml页面使用相同的方法来显示来自实体框架的项目项,似乎工作得很好,但试图使用上面的代码来显示项目的任务不会。我不知道为什么"模型"在项目页面工作正常,"模型"在任务页面启动NullReferenceException错误。我将非常感谢任何帮助!提前感谢!

@mfanto:这是完整的控制器:

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

NullReferenceException in MVC

如果Model为null,则意味着你没有在控制器的操作中设置它。你的代码应该看起来像这样:

public ActionResult EFtest()
{
    IntelliBase.Models.IntelliBaseEntities db = new Models.IntelliBaseEntities();          
    var items = db.Tasks.ToList();
    return View(items);
}

这将设置Model等于项目列表