title: Using view models in ASP.NET MVC 3
body: <p>I'm relatively new to view models and I'm running into a few problems with using them. Here's one situation where I'm wondering what the best practice is...</p>
  
  <p>I'm putting all the information a view needs into the view model. Here's an example -- please forgive any errors, this is coded off the top of my head.</p>
  
  <pre><code>public ActionResult Edit(int id)
  {
      var project = ProjectService.GetProject(id);
  
      if (project == null)
          // Something about not found, possibly a redirect to 404.
  
      var model = new ProjectEdit();
      model.MapFrom(project); // Extension method using AutoMapper.
  
      return View(model);
  }
  </code></pre>
  
  <p>If the screen only allows editing of one or two fields, when the view model comes back it's missing quite a bit of data (as it should be).</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(int id, ProjectEdit model)
  {
      var project = ProjectService.GetProject(id);
  
      if (project == null)
          // Something about not found, possibly a redirect to 404.
  
      try
      {
          if (!ModelState.IsValid)
              return View(model) // Won't work, view model is incomplete.
  
          model.MapTo(project); // Extension method using AutoMapper.
          ProjectService.UpdateProject(project);
          // Add a message for the user to temp data.
  
          return RedirectToAction("details", new { project.Id });
      }
      catch (Exception exception)
      {
          // Add a message for the user to temp data.
  
          return View(model) // Won't work, view model is incomplete.
      }
  }
  </code></pre>
  
  <p>My temporary solution is to recreate the view model from scratch, repopulate it from the domain model, reapply the form data to it, then proceed as normal. But this makes the view model parameter somewhat pointless.</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(int id, ProjectEdit model)
  {
      var project = ProjectService.GetProject(id);
  
      if (project == null)
          // Something about not found, possibly a redirect to 404.
  
      // Recreate the view model from scratch.
      model = new ProjectEdit();
      model.MapFrom(project); // Extension method using AutoMapper.
  
      try
      {
          TryUpdateModel(model); // Reapply the form data.
  
          if (!ModelState.IsValid)
              return View(model) // View model is complete this time.
  
          model.MapTo(project); // Extension method using AutoMapper.
          ProjectService.UpdateProject(project);
          // Add a message for the user to temp data.
  
          return RedirectToAction("details", new { project.Id });
      }
      catch (Exception exception)
      {
          // Add a message for the user to temp data.
  
          return View(model) // View model is complete this time.
      }
  }
  </code></pre>
  
  <p>Is there a more elegant way?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Both answers are correct so I would award them both if I could. The nod goes to MJ however since after trial and error I find his solution to be the leanest.</p>
  
  <p>I'm still able to use the helpers too, Jimmy. If I add what I need to be displayed to the view bag (or view data), like so...</p>
  
  <pre><code>ViewBag.Project= project;
  </code></pre>
  
  <p>I can then do the following...</p>
  
  <pre><code>@Html.LabelFor(model =&gt; ((Project)ViewData["Project"]).Name)
  @Html.DisplayFor(model =&gt; ((Project)ViewData["Project"]).Name)
  </code></pre>
  
  <p>A bit of a hack, and it requires the domain model to be decorated with <code>System.ComponentModel.DisplayNameAttribute</code> in some cases, but I already do that.</p>
  
  <p>I'd love to call...</p>
  
  <pre><code>@Html.LabelFor(model =&gt; ViewBag.Project.Name)
  </code></pre>
  
  <p>But dynamic causes a problem in expressions.</p>