7

I have a very simple page (I've reduced it to almost nothing to see if I could get this working). A NullReferenceException is being thrown when calling Html.TextBoxFor with the lambda shown below. I am trying to work out why this is?

The exception itself seems to be handled as it doesn't stop rendering the page, but I don't think it should be occurring at all? The text box is actually rendered correct as expected (with the regex and placeholder).

If I swap TextBoxFor for PasswordFor a NullReferenceException is also not thrown.

View:

@model Analytics.Sites.Frontend.Models.RegisterViewModel
<div>
    @using (Html.BeginForm("Register", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "centeralign", role = "form" }))
    {
        @Html.TextBoxFor(m => m.Email, new { @placeholder = "email@address.com" })
    }

</div>

ViewModel

public class RegisterViewModel
{
    [Required]
    [Display(Name = "Email")]
    [DataType(DataType.EmailAddress)]
    [RegularExpression(@"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", ErrorMessage = "The email address you entered is not valid.")]
    public string Email { get; set; }
}

Exception:

System.NullReferenceException
   at lambda_method(Closure , RegisterViewModel )

Ideas? Is this by design and it's throwing and catching an NullReferenceException internally?

Edit:

The controller itself is empty.

[Authorize]
public class AccountController : Controller
{
   [AllowAnonymous]
    public ActionResult Register()
    {
        return View();
    }
}

Obviously that is not passing a model instance to the view. Changing to return View(new RegisterViewModel) will negate the NRE. Is that the intended way of doing this? The samples show otherwise.

If you create a new skeleton MVC project with VS2013 it has the same code as this on the Account/Register method. The skeleton project also throws (an handles?) the same NRE. Possible this is by design?

AndySavage
  • 1,583
  • 1
  • 17
  • 31
  • Is the Model or Email null? Im sure you already check that, but had to ask. – bsayegh Mar 31 '14 at 15:23
  • 1
    Can you show your controller method? I think you may not be instantiating something – Robert Mar 31 '14 at 15:23
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jdphenix Mar 31 '14 at 15:26
  • Included controller. I don't think this is a duplicate of the other question. The sample code with VS may just be showing an incorrect practice? – AndySavage Mar 31 '14 at 15:42

2 Answers2

9

You must pass an instance of your model to the view:

 public ActionResult Register()
 {
      return View(new RegisterViewModel());
 }

This is the correct practice in MVC. You cannot access a property on a model without an instance of the model.

Robert
  • 4,084
  • 8
  • 39
  • 81
  • 3
    This implies the VS 2013 sample project (When you do Project -> New) is incorrect (Which it may well be!) as it does not pass a model instance. – AndySavage Mar 31 '14 at 15:46
  • 1
    Why would it throw an Exception only for the TextBoxFor and not the PasswordFor? – MuKa Jul 19 '19 at 14:00
  • @AndySavage It's no different in VS2019. I thought I was going mad having written my own Controller and View so I used the built-in scaffolding feature to add a new Controller and View for the `Create` method and, just like my code, it does not pass an empty model to the View and a Null Reference Exception is thrown. Surely the VS scaffolding isn't wrong even after 7+ years?! – Philip Stratford Apr 23 '20 at 11:25
  • I'm in the same boat, I don't know why this is happening. Like others have said, that's how it was in the VS scaffolding templates. – J86 May 18 '20 at 10:57
1

If your view is rendering and binding to the model then you need to pass a model to the view. If it's the initial GET of the view you should pass an empty model.

Craig W.
  • 16,585
  • 6
  • 44
  • 77