title: ViewModel Implementation in ASP.NET MVC - Is this code best practice?
body: <p>I've just started to get into using ViewModels. Can you guys check out this code to see if I'm following best practice? Is there anything out of the ordinary? Would you do the validation differently? </p>
  
  <p>Sorry if code is lengthy (there's so many parts to it). I've tried to make it as easy to understand as possible.</p>
  
  <p>Thanks!</p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>  public class CustomerModel
    {
      [Required(ErrorMessage="Primer nombre!")]
      public string FirstName { get; set; }
  
      [Required(ErrorMessage="Segundo nombre!")]
      public string LastName { get; set; }
  
      [Required(ErrorMessage="Edad")]
      public int? Age { get; set; }
  
      public string State { get; set; }
      public string CountryID { get; set; }
  
      [Required(ErrorMessage="Phone Number")]
      public string PhoneNumber { get; set; }
    }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>  public class CustomerViewModel
    {
      public CustomerModel Customer { get; set; }
  
      public string Phone1a { get; set; }
      public string Phone1b { get; set; }
      public string Phone1c { get; set; }
    }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>    public ActionResult Index()
      {
        CustomerViewModel Customer = new CustomerViewModel()
        {
          Customer = new CustomerModel(),
        };
  
  
        return View(Customer);
      }
  
  
      [HttpPost]
      public ActionResult Index(CustomerViewModel c)
      {
  
        //ModelState.Add("Customer.PhoneNumber", ModelState["Phone1a"]);
  
        // Let's manually bind the phone number fields to the PhoneNumber properties in
        // Customer object. 
        c.Customer.PhoneNumber = c.Phone1a + c.Phone1b + c.Phone1c;
  
        // Let's check that it's not empty and that it's a valid phone number (logic not listed here)
        if (!String.IsNullOrEmpty(c.Customer.PhoneNumber))
        {
          // Let's remove the fact that there was an error! 
          ModelState["Customer.PhoneNumber"].Errors.Clear();
        } // Else keep the error there. 
  
        if (ModelState.IsValid)
        {
          Response.Write("&lt;H1 style'background-color:white;color:black'&gt;VALIDATED&lt;/H1&gt;");
        }
        return View("Index", c);
      }
  
    }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model MVVM1.Models.CustomerViewModel
  
  @using (Html.BeginForm("Index", "Detail"))
  {  
    &lt;table border="1" cellpadding="1" cellspacing="1"&gt;
      &lt;tr&gt;
        &lt;td&gt;@Html.LabelFor(m =&gt; m.Customer.FirstName)&lt;/td&gt;
        &lt;td&gt;
          @Html.TextBoxFor(m =&gt; m.Customer.FirstName)
          @Html.ValidationMessageFor(m =&gt; m.Customer.FirstName)
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;@Html.LabelFor(m =&gt; m.Customer.LastName)&lt;/td&gt;
        &lt;td&gt;
          @Html.TextBoxFor(m =&gt; m.Customer.LastName)
          @Html.ValidationMessageFor(m =&gt; m.Customer.LastName)
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;@Html.LabelFor(m =&gt; m.Customer.Age)&lt;/td&gt;
        &lt;td&gt;
          @Html.TextBoxFor(m =&gt; m.Customer.Age)
          @Html.ValidationMessageFor(m =&gt; m.Customer.Age)
        &lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td&gt;@Html.LabelFor(m =&gt; m.Customer.PhoneNumber)&lt;/td&gt;
        &lt;td width="350"&gt;
          @Html.TextBoxFor(m =&gt; m.Phone1a, new { size="4", maxlength="3" })
          @Html.TextBoxFor(m =&gt; m.Phone1b)
          @Html.TextBoxFor(m =&gt; m.Phone1c)
          &lt;div&gt;
          @Html.ValidationMessageFor(m =&gt; m.Customer.PhoneNumber)
          &lt;/div&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;
          &lt;input type="submit" value="Submit" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  }
  </code></pre>