0

controller

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

}

}

view on ActionResult Index() method @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>
}

two Model class 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);
    } 

  }
 }

And 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; }

      }

   }

I am getting null reference exception in foreach row

I already tried If(Model != null ) for the foreach loop , butits giving me parser error

Please help .

1 Answers1

0

You should do in a such way:

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

Your Model is null in Index view since you forget pass it in

return View();