title: ASP.NET MVC 4, Multiple model in View, System.NullReferenceException:
body: <p>I have a problem with my App.
  I have This Multiple Model:</p>
  
  <pre><code>public class MultipleModel
  {
      public Staff Staff;
      public RegisterModel RegisterModel;
  }
  
  public class RegisterModel
  {
      [Required]
      [Display(Name = "User name")]
      public string UserName { get; set; }
  
      [Required]
      [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
      [DataType(DataType.Password)]
      [Display(Name = "Password")]
      public string Password { get; set; }
  
      [DataType(DataType.Password)]
      [Display(Name = "Confirm password")]
      [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
      public string ConfirmPassword { get; set; }
  }
  
  
  public partial class Staff
  {
      public Staff()
      {
          this.Tables = new HashSet&lt;Tables&gt;();
      }
  
      public int EmployeeID { get; set; }
      public string First_name { get; set; }
      public string Last_name { get; set; }
      public string Telephone { get; set; }
      public string Description { get; set; }
      public string Username { get; set; }
  
      public virtual ICollection&lt;Tables&gt; Tables { get; set; }
  }
  </code></pre>
  
  <p>The controler is:</p>
  
  <pre><code>    [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public ActionResult MultipleModelView(MultipleModel model)
      {          
          if (ModelState.IsValid)
          {
              try
              {                   
                  WebSecurity.CreateUserAndAccount(model.RegisterModel.UserName, model.RegisterModel.Password);
                  Roles.AddUserToRole(model.RegisterModel.UserName, "Customer");
                  Staff staff = new Staff();
  
                  staff.Username = model.RegisterModel.UserName;
                  staff.First_name = model.Staff.First_name;
                  staff.Last_name = model.Staff.Last_name;
                  staff.Telephone = model.Staff.Telephone;
                  db.Staff.Add(staff);
                  db.SaveChanges();
  
                  WebSecurity.Login(model.RegisterModel.UserName, model.RegisterModel.Password);
  
                  return RedirectToAction("Index", "Home");
              }
              catch (MembershipCreateUserException e)
              {
                  ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
              }
          }
          return View(model);
      }
  </code></pre>
  
  <p>And View is: </p>
  
  <pre><code>@model test13.Models.MultipleModel
  @{
  ViewBag.Title = "MultipleModelView";
  }
  
  &lt;h2&gt;MultipleModelView&lt;/h2&gt;
  @using (Html.BeginForm("MultipleModelView", "Account", FormMethod.Post))
  {
  @Html.AntiForgeryToken()
  @Html.ValidationSummary()
  &lt;fieldset&gt;
      &lt;legend&gt;Registration Form&lt;/legend&gt;
      &lt;ol&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.RegisterModel.UserName)
              @Html.TextBoxFor(m =&gt; m.RegisterModel.UserName)
          &lt;/li&gt;            
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.RegisterModel.Password)
              @Html.PasswordFor(m =&gt; m.RegisterModel.Password)
          &lt;/li&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.RegisterModel.ConfirmPassword)
              @Html.PasswordFor(m =&gt; m.RegisterModel.ConfirmPassword)
          &lt;/li&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.Staff.First_name)
              @Html.TextBoxFor(m =&gt; m.Staff.First_name)
          &lt;/li&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.Staff.Last_name)
              @Html.TextBoxFor(m =&gt; m.Staff.Last_name)
          &lt;/li&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.Staff.Telephone)
              @Html.TextBoxFor(m =&gt; m.Staff.Telephone)
          &lt;/li&gt;
  
      &lt;/ol&gt;
      &lt;input type="submit" value="Register" /&gt;
  &lt;/fieldset&gt;
  
  }
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>
  
  <p>When i Fill data to the TextBoxs and Post them, App get me Exception in this part of code:</p>
  
  <pre><code> WebSecurity.CreateUserAndAccount(model.RegisterModel.UserName, model.RegisterModel.Password);
  </code></pre>
  
  <p>System.NullReferenceException: Object reference not set to an instance of an object.</p>
  
  <p>Help me plese with this. Thank you.</p>