来自数据库的信息表未显示:System.NullReferenceException

时间:2014-01-11 00:48:37

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

所以,我建立了一个数据库连接,创建了一个表并成功添加了一个“View”,这样我就可以访问www.mysite.com/TableView了,所有内容都显示正确。 (使用剃刀“强类型视图”生成我的数据库模型。我使用edmx向导创建数据库连接,并生成一个单独的控制器。

然后我想在index.cshtml文件中显示该表。我将在TableView视图中生成的代码复制到我的index.cshtml中的正确,但它抛出了一个System.NullReferenceException。

我无法理解为什么它不会工作。它确实作为“独立”视图工作,但是当我将其粘贴到另一个cshtml页面时却没有?

我发现它与模型有关,但是当它在另一个视图中工作时,它为什么不起作用。

以下是从工作视图粘贴到我的index.cshtml中的代码:

@{
ViewBag.Title = "Home Page";
Layout = "~/Views/Shared/_Layout.cshtml";
}


<div class="page-full-width cf">
<div class="content-module">

    <div class="content-module-heading cf">

        <h3 class="fl">Full width page</h3>
        <span class="fr expand-collapse-text">Click to collapse</span>
        <span class="fr expand-collapse-text initial-expand">Click to expand</span>
    </div> <!-- end content-module-heading -->


    <div class="content-module-main">

   @model IEnumerable<WebEncode.Models.RunningJobsDb>

<p>
@Html.ActionLink("Create New", "Create")
 </p>
 <table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Title)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Status)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Publish)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.User)
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Status)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Publish)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.User)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>
}

</table>

    </div> <!-- end content-module-main -->

</div> <!-- end content-module -->

这是我的RunningJobsController

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebEncode.Models;

  namespace WebEncode.Controllers
  {
public class RunningJobsController : Controller
{
    private WebEncodeDBEntities db = new WebEncodeDBEntities();

    //
    // GET: /RunningJobs/

    public ActionResult Index()
    {
        return View(db.RunningJobsDb.ToList());
    }


    //
    // GET: /RunningJobs/Details/5

    public ActionResult Details(int id = 0)
    {
        RunningJobsDb runningjobsdb = db.RunningJobsDb.Find(id);
        if (runningjobsdb == null)
        {
            return HttpNotFound();
        }
        return View(runningjobsdb);
    }

    //
    // GET: /RunningJobs/Create

    public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /RunningJobs/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(RunningJobsDb runningjobsdb)
    {
        if (ModelState.IsValid)
        {
            db.RunningJobsDb.Add(runningjobsdb);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(runningjobsdb);
    }

    //
    // GET: /RunningJobs/Edit/5

    public ActionResult Edit(int id = 0)
    {
        RunningJobsDb runningjobsdb = db.RunningJobsDb.Find(id);
        if (runningjobsdb == null)
        {
            return HttpNotFound();
        }
        return View(runningjobsdb);
    }

    //
    // POST: /RunningJobs/Edit/5

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(RunningJobsDb runningjobsdb)
    {
        if (ModelState.IsValid)
        {
            db.Entry(runningjobsdb).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(runningjobsdb);
    }

    //
    // GET: /RunningJobs/Delete/5

    public ActionResult Delete(int id = 0)
    {
        RunningJobsDb runningjobsdb = db.RunningJobsDb.Find(id);
        if (runningjobsdb == null)
        {
            return HttpNotFound();
        }
        return View(runningjobsdb);
    }

    //
    // POST: /RunningJobs/Delete/5

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        RunningJobsDb runningjobsdb = db.RunningJobsDb.Find(id);
        db.RunningJobsDb.Remove(runningjobsdb);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
    }
   }

模特。

命名空间WebEncode.Models    {     使用系统;     使用System.Collections.Generic;

public partial class RunningJobsDb
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Status { get; set; }
    public string Publish { get; set; }
    public string User { get; set; }
    public byte[] Remaining { get; set; }
}
}

我知道有一些关于System.NullReference的问题很安静,但我找不到任何描述我的问题(可能表明我实际上是以愚蠢的方式进行)

3 个答案:

答案 0 :(得分:1)

我找到了解决方案。而且我觉得很愚蠢。我没有以正确的方式考虑控制器和视图。在我读到HomeController负责View in / Home文件夹后,问题出现了。

然后我将内容从我的RunningJobs控制器移动到HomeController。这样我就可以使用HomeController中的ActionResult Index()函数将数据库内容传递到我的“Home / Index.cshtml”页面。

代码本身是正确的,问题是我没有意识到MVC的结构。

答案 1 :(得分:0)

我的猜测是你在控制器动作中设置ActionResult时没有传入模型。代码应该类似于:

返回视图(“ViewTable”,&lt; * IEnumerable实例&lt; RunningJobsDB&gt; *&gt;)

干杯,

的Marius

答案 2 :(得分:0)

您应该删除表格中的第一行。因为您的模型IEnumerable<WebEncode.Models.RunningJobsDb>不仅仅是RunningJobsDb所以如果没有foreach循环就无法执行此操作:

@Html.DisplayNameFor(model => model.Title)

此处,model代表您的IEnumerable,并且不包含Title属性。您可以将它与foreach一起使用,但您已经在使用它。只需删除第一行,它是多余的并导致异常。