-1

WHen Control comes to following helper,object ref error occurs , I don't know why?

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

Model count is not zero and also SundayDriveTime alos has value My model is :@model List<PredefinedDriveTimeDetails>

It does not show error while rendering other text boxes.

what is the problem??

leppie
  • 115,091
  • 17
  • 196
  • 297
S2K
  • 1,185
  • 3
  • 27
  • 55
  • 1
    Show the loop where you use this code. I guess it has to do with the m in `m[i]`, but my crystal ball doesn't help me further. – Raphaël Althaus Jul 09 '14 at 07:08
  • @for (int i = 0; i < Model.Count; i++) {@Html.TextBoxFor(m => m[i].SaturdayDriveTime) @Html.TextBoxFor(m => m[i].SundayDriveTime)} – S2K Jul 09 '14 at 07:09
  • Razor errors can sometimes be a little misleading; it's possible that the actual error is in a very different location than that line of code. Try commenting out that/those `TextBoxFor`s and see if the issue continues to occur? – eouw0o83hf Jul 09 '14 at 07:34

1 Answers1

0

Got your for loop code from comment. what you are doing wrong is referencing m => m[i] which should be m => Model[i]

Try using following code

@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>
}

References

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

Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56