Null reference exception from TextBoxFor lambda in Asp.Net MVC

-5

I'm just doing a little above and beyond debugging, having turned on some addition exception blocking in my project. I'm seeing an exception I've never dealt with before though:

<div class="form-group">
   @Html.LabelFor(model => model.shift_type, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
      @Html.DropDownList("shift_type", null, htmlAttributes: new { @class = "form-control" })
      @Html.ValidationMessageFor(model => model.shift_type, "", new { @class = "text-danger" })
   </div>
</div>
<div class="form-group">
   @Html.LabelFor(model => model.shift_notes, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
      @Html.TextBoxFor(model => model.shift_notes, new {maxlength = 100})<-- Failing Here -->
      @Html.ValidationMessageFor(model => model.shift_notes, "", new { @class = "text-danger" })
   </div>
</div>

The exception message says it's being thrown on the lambda. Everything else on the page uses the same data model, and works fine. So I'm curious what could be the problem. When I paste the exception into notepad++, I get this:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

And here's the model, with the field intact.

enter image description here

c#
asp.net-mvc
entity-framework
asked on Stack Overflow Dec 17, 2018 by Scuba Steve • edited Dec 17, 2018 by pushkin

1 Answer

0

I think the problem is the second attribute is an anonymous object without a collection initializer. Razor is unable to figure out which overload method you are trying to use. Try:

@Html.TextBoxFor(model => model.shift_notes, htmlAttributes: new {maxlength = 100})
answered on Stack Overflow Dec 18, 2018 by Khyron

User contributions licensed under CC BY-SA 3.0