-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??

3
  • 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. Commented Jul 9, 2014 at 7:08
  • @for (int i = 0; i < Model.Count; i++) {<td style="vertical-align: middle;">@Html.TextBoxFor(m => m[i].SaturdayDriveTime)</td> <td style="vertical-align: middle;">@Html.TextBoxFor(m => m[i].SundayDriveTime)</td>} Commented Jul 9, 2014 at 7: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 TextBoxFors and see if the issue continues to occur? Commented Jul 9, 2014 at 7:34

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

@eouw0o83hf: Please see my added references.
Wow, color me surprised. Deleting my previous remark because it's apparently just wrong and misleading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.