title: How to Edit and Save ViewModels data back to database
body: <p>I have a ViewModel which is joined by three Entities to get data from all entities into one view form. Although i succeeded to implement the same. But i have no idea how to Edit and Save data back to the database. My model classes are joined by one to one relationship.</p>
  
  <p>My Models are:</p>
  
  <pre><code>public class Doctor
  {
      public int DoctorId { get; set; }
      public string Name { get; set; }
      public string Speciality { get; set; }
  
      public virtual DoctorAddress DoctorAddress { get; set; }
      public virtual DoctorCharge DoctorCharge { get; set; }
      public virtual DoctorAvailablity DoctorAvailablity { get; set; }
  
  }
  
  public class DoctorAddress
  {
      public string Address { get; set; }
      public string City { get; set; }
      public int DoctorId { get; set; }
  
      public virtual Doctor Doctor { get; set; }
  }
  
  public class DoctorCharge
  {
      public decimal OPDCharge { get; set; }
      public decimal IPDCharge { get; set; }
      public int DoctorId { get; set; }
  
      public virtual Doctor Doctor { get; set; }
  }
  </code></pre>
  
  <p>My ViewModel is: </p>
  
  <pre><code>public class DoctorViewModel
      {
          public Doctor Doctor { get; set; }
          public DoctorAddress DoctorAddress { get; set; }
          public DoctorCharge DoctorCharge { get; set; }
  
      }
  </code></pre>
  
  <p>My Controller is: </p>
  
  <pre><code>public ActionResult Index()
      {
          var model = from t1 in db.Doctors
                      join d in db.DoctorAddress on t1.DoctorId equals  d.DoctorId into listi
                      join dc in db.DoctorCharges on t1.DoctorId equals dc.DoctorId into listj
  
                      from d in listi.DefaultIfEmpty()
                      from dc in listj.DefaultIfEmpty()
  
                      select new DoctorDetailsViewModel.DoctorViewModel { Doctor = t1, DoctorAddress = d, DoctorCharge = dc };
  
          return View(model.ToList());
  
      }
  </code></pre>
  
  <p>My View is: </p>
  
  <pre><code>@model XXX.DoctorDetailsViewModel.DoctorViewModel
  
  @using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  
  &lt;fieldset&gt;
      &lt;legend&gt;Doctor&lt;/legend&gt;
  
      &lt;div class="editor-label"&gt;
         Name
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.Doctor.Name)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          OPD Charge
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.DoctorCharge.OPDCharge)
      &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
          Address
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.DoctorAddress.Address)
      &lt;/div&gt; &lt;p&gt;
          &lt;input type="submit" value="Create" /&gt;
      &lt;/p&gt;
  &lt;/fieldset&gt;}
  </code></pre>
  
  <p>My Controller Class is:</p>
  
  <pre><code>public ActionResult Create()
      {
          return View();
      }
  
      //
      // POST: /Doctor/Create
  
      [HttpPost]
      public ActionResult Create(Doctor doctor)
      {
          if (ModelState.IsValid)
          {
              db.Doctors.Add(doctor);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          return View(doctor);
      }
  </code></pre>
  
  <p>Please help me how do i do. Thanks in advance.</p>