albert albert

albert albert

  • NA
  • 524
  • 0

index view of asp.net mvc3

Aug 2 2012 4:23 AM
Hi everybody,

I have this:

The view: Index.cshtml

[code]
@model ManyToMany.ViewModels.ProjectIndexData
@{
  ViewBag.Title = "Hours";
}

<h2>Hours</h2>

<p>
  @Html.ActionLink("Create New", "Create")
</p>
 <table style="width: 100%;">
  <tr>
  <th>Project</th>
  <th>Activity</th>
  <th></th> 
 
  </tr>

@foreach (var item in Model.Hours) {
  <tr>
 
  <td>
  @Html.DisplayFor(modelItem => item.Activity.Project.Name) 
  </td>
 
  <td>
 
  @Html.DisplayFor(modelItem => item.Activity.Name)
  </td>
 
  <td>
  @Html.ActionImage("Edit", new {id = item.HourID},"~/Content/Images/Edit2.png", "Edit" )
  @Html.ActionImage("Details", new{id = item.HourID}, "~/Content/Images/details_icon.gif", "Details")
  @Html.ActionImage("Delete", new{id = item.HourID}, "~/Content/Images/Delete.png", "Delete")
  </td>
  </tr>
}

</table>

[/code]

and the Controller:
HourController:

[code]
 public ActionResult Index(Int32? id)
  {
 
  var viewModel = new ProjectIndexData();
  ViewBag.Projects = db.Projects.ToList();
  ViewBag.Activities = db.Activities.ToList();
  //ViewBag.Hours = db.Hours.ToList();
  //ViewBag.HourID = id.Value;
  var Hours = db.Hours.Include(a => a.Activity).Include(a => a.Activity.Project);
  return View(viewModel);


  }//end method Index
[/code]

and the ProjectIndexData looks like this:

[code]
 public class ProjectIndexData
  {
  public IEnumerable<Project> Projects { get; set; }
  public IEnumerable<Employee> Employees { get; set; }
  public IEnumerable<Activity> Activities { get; set; }
  public IEnumerable<Hour> Hours { get; set; }
  public Activity Activity { get; set; }

  }
[/code]

but I get thsi error:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 17: </tr> Line 18: Line 19: @foreach (var item in Model.Hours) { Line 20: <tr> Line 21:

Source File: f:\WinituProjects21-07-2012\winituProjects08-07-2012\ManyToMany26-05-2012WorkingVersionWorkingVersion03 - Copy\ManyToMany\Views\Hour\Index.cshtml   Line: 19

THX for helping

Can annybody help me??

THX!!