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; }
}
}
@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>
}
}
///编辑。
和来自控制器
public Employer Controller
{
public ActionResult Timesheet()
{
return View();
}
}
这就像
那样出了什么问题 "Object reference not set to reference of the object"
我正在从模型类调用列表并返回&#34; count&#34;列表的元素,它应该返回否。列表中的元素,但返回null引用。请帮助我,如何修复?
答案 0 :(得分:3)
你没有初始化模型数据并返回带有空模型的View,这就是为什么它会给你错误,因为对象没有实例化。
你必须像这样实例化它:
public Employer Controller
{
public ActionResult Timesheet()
{
GetTimesheetList model = new GetTimesheetList();
model.GetTimesheetDetails = new List<TimesheetModel>();
return View(model);
}
}
答案 1 :(得分:0)
Model.GetTimesheetDetails
是null
。您需要使用new
关键字创建实例。
答案 2 :(得分:0)
问题是您在未初始化的情况下引用GetTimesheetDetails
,因此当GetTimesheetDetails.Count
GetTimesheetDetails
为空时
答案 3 :(得分:0)
更新您的控制器方法,如下所述:
public Employer Controller
{
public ActionResult Timesheet()
{
return View(new GetTimesheetList{
GetTimesheetDetails = new List<TimesheetModel>()
});
}
}
注意:这将返回您的班级GetTimesheetList
的新实例。它不会给你任何错误,但它不会通过循环,因为它没有任何数据。
答案 4 :(得分:0)
您没有将模型返回到您的视图,视图接受模型。
您的观点已定义
@model Project1.Models.GetTimesheetList
并且在视图中您尝试访问此模型。尝试使用它的第一行是Model.GetTimesheetDetails.Count
,因为没有传递模型Model.GetTimesheetDetails
为空,因此它会引发异常。
您需要将模型传递给视图,例如......
public Employer Controller
{
public ActionResult Timesheet()
{
// get model from somewhere;
return View(model);
}
}
如果您需要传递空模型,这将有所帮助
public ActionResult Timesheet()
{
var model = new GetTimesheetList();
model.GetTimesheetDetails = new List<TimesheetModel>();
return View(model);
}
但我怀疑是你的情况,因为这样你的for循环就会被跳过
Model.GetTimesheetDetails.Count
现在不会抛出错误但是为零并跳过循环。