9

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 protected]" })
    }

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

3
  • Is the Model or Email null? Im sure you already check that, but had to ask. Commented Mar 31, 2014 at 15:23
  • 1
    Can you show your controller method? I think you may not be instantiating something Commented Mar 31, 2014 at 15:23
  • 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? Commented Mar 31, 2014 at 15:42

2 Answers 2

14

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.

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

4 Comments

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.
Why would it throw an Exception only for the TextBoxFor and not the PasswordFor?
@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?!
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.