title: Submitting a ViewModel Derived Form in ASP.NET MVC 4
body: <p>I want to create a form that is comprised of fields that come from separate models and then, when submitted, updates the corresponding tables in the database. </p>
  
  <p>I couldn't find the answer anywhere so I tinkered with things until I made something work. Being new to all of this, however, I doubt the efficacy of my solution. I'm hoping that by stating my problem and showing my solution you can better understand what I need and be able to help me find the best solution for my problem.</p>
  
  <p>I like simple, abstract examples so here is what I currently have working:</p>
  
  <p>First, I make a Color Model...</p>
  
  <pre><code>namespace example.Models
  {
      public class Color
      {
          public int ColorID { get; set; }
          public string Name { get; set; }
      }
  }
  </code></pre>
  
  <p>Then I make a Shape Model...</p>
  
  <pre><code>namespace example.Models
  {
      public class Shape
      {
          public int ShapeID { get; set; }
          public string Name { get; set; }
      }
  }
  </code></pre>
  
  <p>Next, I make a simple ViewModel...</p>
  
  <pre><code>namespace example.ViewModels
  {
      public class ColorAndShapeViewModel
      {
          public Color Color { get; set; }
          public Shape Shape { get; set; }
      }
  }
  </code></pre>
  
  <p>Here is my DbContext...</p>
  
  <pre><code>namespace example.DAL
  {
      public class MyContext : DbContext
      {
          public DbSet&lt;Color&gt; Colors { get; set; }
          public DbSet&lt;Shape&gt; Shapes { get; set; }
      }
  }
  </code></pre>
  
  <p>Next, the Controller...</p>
  
  <pre><code>namespace example.Controllers
  {
      public class MyController : Controller
      {
          private MyContext db = new MyContext();
  
          public ActionResult Index()
          {
              return View();
          }
  
          public ActionResult Create()
          {
              return View();
          }
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create(ColorAndShapeViewModel viewmodel)
          {
              Color newColor = new Color();
              newColor.Name = viewmodel.Color.Name;
  
              Shape newShape = new Shape();
              newShape.Name = viewmodel.Shape.Name;
  
              db.Colors.Add(newColor);
              db.Shapes.Add(newShape);
              db.SaveChanges();
  
              return RedirectToAction("Index");
          }
  
      }
  }
  </code></pre>
  
  <p>Lastly, the View...</p>
  
  <pre><code>@model example.ViewModels.ColorAndShapeViewModel
  
  @using (Html.BeginForm()) {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
      @Html.LabelFor(model =&gt; model.Color.Name)&lt;br /&gt;
      @Html.TextBoxFor(model =&gt; model.Color.Name)&lt;br /&gt;
      &lt;br /&gt;
      @Html.LabelFor(model =&gt; model.Shape.Name)&lt;br /&gt;
      @Html.TextBoxFor(model =&gt; model.Shape.Name)&lt;br /&gt;
      &lt;br /&gt;
      &lt;input type="submit" value="Create" /&gt;
  }
  </code></pre>
  
  <p>Thus far, everything works as expected: I am able to enter data for separate models into a single form and submit it. The database is updated (the color goes into the color table, the shape into the shape table) and all is well—or is it? In reality, I will have a much longer form using several models which are related to each other. My present solution would require a large controller, full of <code>newObject.Property = viewModel.Object.Property</code> statements. Isn't that undesirable? Is there a better way to handle this?</p>
  
  <p>Thanks!</p>