3

I Have Model Class

 namespace Project1.Models
 {
  public class GetTimesheetList
  {
   public List<TimesheetModel> GetTimesheetDetails { get; set; }
  }
  public class TimesheetModel
  {
    ResLandEntities res = new ResLandEntities();

    public int WEEK_CAL_ID { get; set; }
    public int COMP_ID { get; set; }
    public int RES_ID { get; set; }
    public int PROJ_ID { get; set; }
    public string DESCR { get; set; }
    public int TEXTBOX_WEEK_ID { get; set; }

    public int EMP_ID { get; set; }


    public int SUN_HRS { get; set; }


    public int MON_HRS { get; set; }


    public int TUE_HRS { get; set; }


    public int WED_HRS { get; set; }


    public int THU_HRS { get; set; }


    public int FRI_HRS { get; set; }


    public int SAT_HRS { get; set; }


    public string START_DATE { get; set; }

    public string END_DATE { get; set; }

    public string IS_DELETED { get; set; }

    public string CR_BY { get; set; }

   }
  }

and In View I Have written like

   @model Project1.Models.GetTimesheetList
   @using (Html.BeginForm("Timesheet", "Employer", FormMethod.Post))

  {

     @Html.AntiForgeryToken()
     @Html.ValidationSummary(true)
     <table class="list-chiller-record">
     @for (int i = 0; i < Model.GetTimesheetDetails.Count; i++)// GETTING NULL REFERENCE HERE.
     {
       if (i == 0)
       {
         <tr class="chiller-record-template" style="display: none">
           <td>@Html.TextBoxFor(m => m.GetTimesheetDetails[i].SUN_HRS, new { style = "width:50px; height:30px;", @class = "sunhrs" })
            </td>
           <td>@Html.TextBoxFor(m => m.GetTimesheetDetails[i].MON_HRS, new { style = "width:50px; height:30px;", @class = "monhrs" })
            </td>
           <td>@Html.TextBoxFor(m => m.GetTimesheetDetails[i].TUE_HRS, new { style = "width:50px; height:30px;", @class = "tuehrs" })
             </td>
           <td>@Html.TextBoxFor(m => m.GetTimesheetDetails[i].WED_HRS, new { style = "width:50px; height:30px;", @class = "wedhrs" })
             </td>
           <td>@Html.TextBoxFor(m => m.GetTimesheetDetails[i].THU_HRS, new { style = "width:50px; height:30px;", @class = "thurhrs" })
            </td>
           <td>@Html.TextBoxFor(m => m.GetTimesheetDetails[i].FRI_HRS, new { style = "width:50px; height:30px;", @class = "frihrs" })
            </td>
           <td>@Html.TextBoxFor(m => m.GetTimesheetDetails[i].SAT_HRS, new { style = "width:50px; height:30px;", @class = "sathrs" })
             </td>
            </tr>    
        }
      }

///Edited.

and From Controller

      public Employer Controller
       {
         public ActionResult Timesheet()
         {
           return View();
         }
       }

What is wrong in this getting like

          "Object reference not set to reference of the object"

I am Calling List from Model Class and returning "count" of the elements of the list, it should return no. of the elements in the list, but returning null reference instead. Please help me anyone, How do I Fix it ??

Sanjay
  • 1,146
  • 2
  • 17
  • 39

5 Answers5

3

you are not initilzing Model data and returning View with empty Model thats why it is giving you error, because object is not instantiated.

You have to instantiate it like this:

public Employer Controller
{
   public ActionResult Timesheet()
   {
       GetTimesheetList model = new GetTimesheetList();
       model.GetTimesheetDetails = new List<TimesheetModel>();
       return View(model);
   }
}
Ehsan Sajjad
  • 59,154
  • 14
  • 90
  • 146
  • Oh thanks..!! and I have tried with your answer getting Error like The "model item passed into the dictionary is of type 'Project1.Models.GetTimesheetList', but this dictionary requires a model item of type 'Project1.Models.TimesheetModel'." @Ehsan Sajjad – Sanjay Apr 09 '14 at 08:35
  • in you view you need to set this model as in your question post it is: model Project1.Models.GetTimesheetList not model Project1.Models.TimesheetModel – Ehsan Sajjad Apr 09 '14 at 08:40
0

Model.GetTimesheetDetails is null. You need to create an instance using the new keyword.

nvoigt
  • 61,531
  • 23
  • 73
  • 116
0

The problem is that you reference GetTimesheetDetails without initialising it, so when you do GetTimesheetDetails.Count GetTimesheetDetails is null

Mike Norgate
  • 2,173
  • 3
  • 19
  • 40
0

Update your controller method as mentioned below :

public Employer Controller
{
    public ActionResult Timesheet()
    {
        return View(new GetTimesheetList{
        GetTimesheetDetails = new List<TimesheetModel>()            
        });
    }
}

Note: This will return a new instance of your class GetTimesheetList. It will not give any error to you but it will not go through loop as it does not have any data.

SpiderCode
  • 9,664
  • 1
  • 19
  • 41
  • sorry, getting same Error like The "model item passed into the dictionary is of type 'Project1.Models.GetTimesheetList', but this dictionary requires a model item of type 'Project1.Models.TimesheetModel'." @SpiderCode – Sanjay Apr 09 '14 at 08:38
  • Right click on the Method name **Timesheet** and select `go to view`. and check what object does it accept ? if it is `@model Project1.Models.TimesheetModel` then return new instance of the timesheet model. like `return View(new List())` – SpiderCode Apr 09 '14 at 08:42
0

You are not returning model to your view, where as your view accepts one.

Your view has this defined

@model Project1.Models.GetTimesheetList

and in the view you have tried to access this model. The first line where its trying to use it is Model.GetTimesheetDetails.Count since no model is passed Model.GetTimesheetDetails is null and hence it throws exception.

You will need to pass a model to the view something like...

public Employer Controller
{
    public ActionResult Timesheet() 
    {
        // get model from somewhere;
        return View(model);
    }
}

If you need to pass an empty model this will be helpful

public ActionResult Timesheet() 
{
    var model = new GetTimesheetList();
    model.GetTimesheetDetails = new List<TimesheetModel>();
    return View(model);
}

but that I doubt will be your case, because with this your for loop would be skipped since Model.GetTimesheetDetails.Count would now not throw error but be zero and skip the loop.

Yasser Shaikh
  • 44,064
  • 44
  • 190
  • 271
  • Yes, that's correct, my model is returning empty, i need created textboxes for all elements in list, how can i do? @Yasser – Sanjay Apr 09 '14 at 08:51