获得类似于“;对象引用未设置为“对象的引用”;在Asp.net MVC中
本文关键字:对象的引用 Asp net 引用 MVC 对象 对象引用 类似于 设置 | 更新日期: 2023-09-27 17:57:28
我有模型类
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"
我从模型类调用List并返回列表中元素的"count",它应该返回列表中的元素数量,但返回null引用。请帮助我,我该如何修复??
您没有初始化Model数据并返回空Model的View,这就是它给您错误的原因,因为对象没有实例化。
你必须像这样实例化它:
public Employer Controller
{
public ActionResult Timesheet()
{
GetTimesheetList model = new GetTimesheetList();
model.GetTimesheetDetails = new List<TimesheetModel>();
return View(model);
}
}
Model.GetTimesheetDetails
就是null
。您需要使用new
关键字创建一个实例。
问题是您引用GetTimesheetDetails
而没有初始化它,所以当您执行GetTimesheetDetails.Count
时,GetTimesheetDetails
为空
如下所述更新控制器方法:
public Employer Controller
{
public ActionResult Timesheet()
{
return View(new GetTimesheetList{
GetTimesheetDetails = new List<TimesheetModel>()
});
}
}
注意:这将返回类GetTimesheetList
的一个新实例。它不会给您任何错误,但它不会通过循环,因为它并没有任何数据。
您不会将模型返回到视图中,因为您的视图接受一个模型。
您的视图已定义
@model Project1.Models.GetTimesheetList
在视图中,您已尝试访问此模型。它尝试使用它的第一行是Model.GetTimesheetDetails.Count
,因为没有传递模型Model.GetTimesheetDetails
为null,因此它抛出异常。
您需要将模型传递给视图,类似于。。。
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
现在不会抛出错误,而是为零并跳过循环。