在MVC中呈现文本框时,对象引用未设置为对象的实例

时间:2014-07-09 07:06:29

标签: .net asp.net-mvc

WHen Control来到下面的帮助器,发生了对象引用错误,我不知道为什么?

 @Html.TextBoxFor(m => m[i].SundayDriveTime, new {@class = "smallTxtEntry0 daysDriveTime sundayDriveTime", @id = "PredefinedDriveTimeDetailsList_" + i + "_SundayDriveTime", oldValue = Model[i].SundayDriveTime, @onchange = "driveTimeDayValuesOnchnge(this)" })</td>

模型计数不为零,而且SundayDriveTime也有值 我的模型是:@model List<PredefinedDriveTimeDetails>

渲染其他文本框时不显示错误。

问题是什么?

1 个答案:

答案 0 :(得分:0)

从评论中获取你的循环代码。您所做错的是引用m => m[i] m => Model[i]

尝试使用以下代码

@for (int i = 0; i < Model.Count; i++)
{
    <td style="vertical-align: middle;">@Html.TextBoxFor(m => Model[i].SaturdayDriveTime)</td>
    <td style="vertical-align: middle;">@Html.TextBoxFor(m => Model[i].SundayDriveTime)</td>
}

参考

ASP.NET MVC 4 - for loop posts model collection properties but foreach does not

MVC with TextBoxFor having same id within loop

MVC 3 - Model Binding a list in a table with each record being a column instead of row