2013-03-01 81 views
-2

'/'应用程序中的服务器错误。我有和对象引用中的错误未设置为对象的实例

未将对象引用设置为对象的实例。

描述:在执行 当前Web请求期间发生未处理的异常。请查阅 的堆栈跟踪,了解有关该错误的更多信息以及它在 代码中的起源。

异常详细信息:System.NullReferenceException:对象引用不是 设置为对象的实例。

Source Error: 


Line 29:   <th></th> 
Line 30:  </tr> 
Line 31:  @foreach (var sections in Model.Sections) 
Line 32:  { 
Line 33:   <tr> 

我的模型

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace TechFactorsLMSV2.Models 
{ 

public class School 
{ 
    public int ID { get; set; } 
    public string SchoolName { get; set; } 
    public ICollection<Section> Sections { get; set; } 
} 
public class Section 
{ 
    public int ID { get; set; } 
    public string SectionName { get; set; } 
    public ICollection<Student> Students { get; set; } 
} 

public class Student 
{ 
    public int ID { get; set; } 
    public string LastName { get; set; } 
    public string FirstName { get; set; } 
    public string MiddleName { get; set; } 
    public string Address { get; set; } 
    public DateTime DateEnrolled { get; set; } 

} 

public class LMSDBContext : DbContext 
{ 
    public DbSet<School> Schools { get; set; } 
    public DbSet<Section> Sections { get; set; } 
    public DbSet<Student> Students { get; set; } 
} 
} 

我控制器

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

namespace TechFactorsLMSV2.Controllers 
{ 
public class SchoolsController : Controller 
{ 
    private LMSDBContext db = new LMSDBContext(); 

    // 
    // GET: /Schools/ 

    public ActionResult Index() 
    { 
     return View(db.Schools.ToList()); 
    } 
public ActionResult Detail(int id) 
    { 
     var model = db.Schools.Single(d => d.ID == id); 
     return View(model); 
    } 
} 
} 

我看来

TechFactorsLMSV2.Models.School 

@{ 
ViewBag.Title = "Detail"; 
} 

<h2>@Model.SchoolName</h2> 

@Html.ActionLink("Add Section", "Create", "Section", new { SchoolId = @Model.ID }, null  

} 

<table> 
    <tr> 
    <th>Sections</th> 
    <th></th> 
    </tr> 
    @foreach (var sections in Model.Sections) 
{ 
    <tr> 
     <td>@sections.SectionName</td> 
     <td> 


     </td> 

    </tr> 
} 


</table> 
+1

你消除了你的代码吗?哪一行引发此异常?看看StackTrace .. – 2013-03-01 08:33:04

+0

So Model.Sections为空?或模型为空? – 2013-03-01 08:33:24

+0

即时通讯新的mvc 4主要错误是在@foreach(var部分在Model.Sections),但我已经宣布已在th model.sections在我的模型 – user2122729 2013-03-01 08:35:42

回答

0

如果你添加一个构造函数到你的学校类上,那么列表中的消息就会消失,例如;

public class School 
{ 
    public int ID { get; set; } 
    public string SchoolName { get; set; } 
    public ICollection<Section> Sections { get; set; } 

    public School() 
    { 
     Sections = new List<Section>(); 
    } 
} 

但作为@Peter说,你需要考虑一下是怎么回事进行填充,并通过一切不是那么回事。

+0

我是goind在我的模型类中添加该类的? – user2122729 2013-03-01 09:08:01

+0

你没有模型类,你有一个模型命名空间,其中有几个类。如果你在每个类的构造函数中新建列表,它不会抛出这个异常,这些列表将是空的,因为你不填充它们......但它不会抛出异常。所以,如果你将上面的构造函数添加到你的学校类中,那么错误应该发生。 – 2013-03-01 09:11:56

+0

ive添加了它的工作正常的构造函数,但如果我将创建该部分的创建页面仍然会正常工作? – user2122729 2013-03-01 09:18:20

0

我会建议懒惰读了/渴望加载。例如,你的部分从来没有加载到模型中...

+0

我能做些什么,谢谢! – user2122729 2013-03-01 08:56:50

+0

@ user2122729查看本视频教程:http://pluralsight.com/training/Courses/TableOfContents/mvc4-building 在“使用数据(第II部分)”部分中特别列出了“列出评论”。它会给你答案和解释。我可以推荐整个系列,如果你想更全面地介绍MVC4 – Peter 2013-03-01 09:12:35

相关问题