title: Web.API, ASP .NET MVC and ModelState error mapping
body: <p>To give a bit of context first, we have an MVC application plugged to a Web API which itself communicate with at least a SQL Sever DB via Entity Framework; communication goes both way as users can edit the data.</p>
  
  <p>There is validation, via data annotation, on all the layers, that's is Db Models, DTOs and ViewModels, which is for the most part the same on every layer, non null-able, minimum length, that sort of thing. We do however have some peculiar, complex, ever changing business logic that we decided to implement only in the Web API. </p>
  
  <p>The validation errors are returned from the Web API using:</p>
  
  <pre><code>Request.CreateErrorResponse(System.Net.HttpStatusCode.OK, ModelState);
  </code></pre>
  
  <p>Some custom header allow the HttpClient's wrapper on the MVC side to handle the response as an error cast the content by into a ModelStateDictionnary</p>
  
  <p>My question(s) relate to the ModelState error targeted property name, since DTOs and the ViewModel aren't exactly the same, I need to map the ModelState error so it will point to the ViewModel property instead of the DTO, for example, if I receive the following ModelState from the Web API :</p>
  
  <pre><code>{"CustomerDTO.full_name", "The full name needs to be unique"}
  </code></pre>
  
  <p>I need it to become:</p>
  
  <pre><code>{"CustomerViewModel.FullName", "The full name needs to be unique"}
  </code></pre>
  
  <p>So it can be displayed at the right place on the views. </p>
  
  <p>At the moment I'm doing this manually, like the initial DTO to ViewModel object mapping, and ,unless my Google-fu teachings failed me, there doesn't seem to be any way in .NET to do so automatically, arguably there is no object mapping either, but I was planning on trying AutoMapper to map the objects together and then reuse the mapping for the ModelState errors. </p>
  
  <p>Now before I waste 3 days on this, probably banging my head on the desk a couple of time in the process, has anybody encountered that problem before ? Does that look like a sound approach ? If not is there a better solution ?  </p>
  
  <p>Thanks</p>