foreach行的ASP .MVC null引用错误

时间:2016-03-07 18:01:19

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

控制器

namespace CodeFirstApproach.Controllers
 {
   public class StudentController : Controller
   {
    //
    // GET: /Student/
    StudentContent objContext;
    public StudentController()
    {
         objContext=new StudentContent();
    }


    public ActionResult Index()
    {
        var students = objContext.students.ToList();
        return View();
    }

    public ViewResult Details(int ID)

    {
        Student student = objContext.students.Where(x => x.ID == ID).SingleOrDefault();
        return View(student);
    }
    #region Create Student
    public ActionResult Create()
    {
        return View(new Student());        
    }
    [HttpPost]
    public ActionResult Create(Student student)
    {
        objContext.students.Add(student);
        objContext.SaveChanges();
        return RedirectToAction("Index");
    }
    #endregion
    #region Edit Student
    public ActionResult Edit(int ID)

    {
        Student student = objContext.students.Where(x => x.ID == ID).SingleOrDefault();
        return View(student);
    }
    [HttpPost]
    public ActionResult Edit(Student model)
    {
        Student student = objContext.students.Where(x => x.ID == model.ID).SingleOrDefault();
        if (student != null)

        {
            objContext.Entry(student).CurrentValues.SetValues(model);
            objContext.SaveChanges();
            return RedirectToAction("Index");


        }

        return View(model);

    }
    #endregion
    #region Delete Student

    public ActionResult Delete(int id)

    {
        Student student = objContext.students.Find(id);
        return View(student);

    }

    [HttpPost]
    public ActionResult Delete(int id, Student model)
    { var student = objContext.students.Where(x => x.ID == id).SingleOrDefault();
    if (student != null)
    {
        objContext.students.Remove(student);
        objContext.SaveChanges();


    }
    return RedirectToAction("Index");

    }

    #endregion

}

}

观察ActionResult Index()方法@model

@model IEnumerable<CodeFirstApproach.Models.Student>

@{
  ViewBag.Title = "Index";
 }

 <h2>Index</h2>
 <p>
     @Html.ActionLink("create new ", "Create");
 </p>
  <table style="width:100%;">
<tr>
    <th style="width:20%;">
        @Html.DisplayNameFor(model=>model.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model=>model.Email)
    </th>
    <th>
        @Html.DisplayNameFor(model=>model.Class)
    </th>
    <th>
        @Html.DisplayNameFor(model=>model.Address)
    </th>
    <th>
        @Html.DisplayNameFor(model=>model.Mobile)
    </th>


</tr>
  @foreach (var item in Model)
{ 
 <tr>
     <td>
         @Html.DisplayFor(modelItem => item.Name)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.Email)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.Class)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.Address)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.Mobile)
     </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>
}

两个Model类 StudentContent.cs

namespace CodeFirstApproach.Models
{
public class StudentContent:DbContext
{
    public StudentContent()
        : base("name=DBConnectionString")
    { 

    }
    public DbSet<Student> students { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>().HasKey(b => b.ID);
        modelBuilder.Entity<Student>().Property(b =>     b.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        base.OnModelCreating(modelBuilder);
    } 

  }
 }

和Student.cs

      namespace CodeFirstApproach.Models
{
public class Student
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Class { get; set; }
    public string Address { get; set; }
    public string Mobile { get; set; }

      }

   }

我在foreach行中获得空引用异常

我已经为foreach循环尝试了If(Model!= null),但是给了我解析器错误

请帮忙。

1 个答案:

答案 0 :(得分:0)

你应该这样做:

    public ActionResult Index()
    {
       var students = objContext.students.ToList();
       return View(students); // Not return View(); !!!
    }

您的模型在索引视图中为空,因为您忘记了在

中传递它
  

返回View();