title: Binding two tables in model - ASP.NET MVC Razor
body: <p>I am facing problem when binding model with two tables in one class. Form is not binding values to model when posting it to controller.</p>
  
  <p>Model:</p>
  
  <pre><code>public class BasicProfileModel {
      public BasicProfileModel() {
          this.user = new User();
          this.basicProfile = new BasicProfile();
      }
      public User user { get; set; }
      public BasicProfile basicProfile { get; set; }        
  }
  </code></pre>
  
  <p>User and Basic Profile classes are two tables with following properties:</p>
  
  <pre><code>public class User {
      public string UserId { get; set; }//not null and PK
      public string EmailId { get; set; }//no null
      public string Salt { get; set; }//not null
      public string Password { get; set; }//not null
      public string Role { get; set; }//not null
      public bool HasCompleteProfile { get; set; }//not null
  }
  
  public class BasicProfile {
      public string UserId { get; set; }//not null FK
      public string FirstName { get; set; }//not null
      public string LastName { get; set; }
      public string Gender { get; set; }//not null
      public int YearOfBirth { get; set; }//not null
      public bool IsApprovedByUser { get; set; }//not null
      public bool IsApprovedByAdmin { get; set; }//not null
      public bool IsActiveByUser { get; set; }//not null
      public bool isActiveByAdmin { get; set; }//not null
  }
  </code></pre>
  
  <p>When I load view model shows any value initialised in controller (that generates view) or in top of the view c# code section.
  But when I post the form the controller shows model without any value at all.
  When I create controllers/views for single table then every thing works fine. Here is code for controller action methods and view.</p>
  
  <pre><code>public ActionResult BasicRegister() {
          BasicProfileModel model = new BasicProfileModel();
  
          //any value initialised in model here displays in view elements e.g. text boxes.
          return View("BasicRegister", model);
      }
  
      [HttpPost]
      public ActionResult DoBasicRegister(BasicProfileModel basicProfile) {
          return View("BasicRegister", basicProfile);
      }
  </code></pre>
  
  <p>//This is view</p>
  
  <pre><code>@model BasicProfileModel
  
  
  @using (Html.BeginRouteForm("DoBasicRegister", FormMethod.Post)) {
  &lt;div&gt;
      @Html.TextBoxFor(m =&gt; m.user.EmailId)
  &lt;/div&gt;
  &lt;div&gt;
      @Html.TextBoxFor(m =&gt; m.user.Password)
  &lt;/div&gt;
  &lt;div&gt;
      @Html.TextBoxFor(m =&gt; m.basicProfile.FirstName)
  &lt;/div&gt;
  
  &lt;div&gt;
      @Html.TextBoxFor(m =&gt; m.basicProfile.LastName)
  &lt;/div&gt;
  &lt;div&gt;
      &lt;input type="submit" value="send" /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>}</p>
  
  <p>//Routes</p>
  
  <pre><code>routes.MapRoute(
              name: "BasicRegister",
              url: "join",
              defaults: new { controller = "Home", action = "BasicRegister" }
              );
  
          routes.MapRoute(
              name: "DoBasicRegister",
              url: "dojoin",
              defaults: new { controller = "Home", action = "DoBasicRegister" }
              );
  </code></pre>