title: ASP.NET MVC Model Validation Error Localization Context
body: <p>First of all, I have to say that I understand how Data Annotation -based Model Validation works in ASP.NET MVC4 and I have it successfully implemented with <code>DataAnnotationsModelValidatorProvider</code>. So I don't need assistance on setting it up.</p>
  
  <p>But when it comes down to <code>HtmlHelpers</code>, I'm struggling with trying to figure the context of the error message. And by saying <code>context</code>, I mean which error we're talking about. Which Attribute returned the error?</p>
  
  <p>What I can get, is the <code>Key</code> for the error and the current <code>ErrorMessage</code> but programmatically, there's nothing, that at least I'm aware of, that would communicate which error we're talking about. Whether it was <code>Required</code> attribute or some other attribute, there's not way that I can find how to distinguish them.</p>
  
  <p>Let's open the scenario a little bit. I have custom <code>HtmlHelpers</code> to render <code>ContentEditable</code> elements. For example <code>Html.ContentEditableValidationMessageFor(m =&gt; m.firstName);</code>. It will output something like this:</p>
  
  <pre><code>&lt;span contenteditable="true" data-valmsg-for="firstName" data-valmsg-replace="Please provide first name" class="field-validation-error"&gt;Please provide first name&lt;/span&gt;
  </code></pre>
  
  <p>Now, I do have a jQuery plugin to handle and persist the changes in the <code>contenteditable</code> element and it will persist them into the backend. However, the UI has nothing that would say which error message we're talking about. Humans can easily see it's the <code>RequiredAttribute</code>, but programmatically there's no data to differentiate it from some <code>MinLengthAttribute</code> for example.</p>
  
  <p>In this scenario, if I would simply use the <code>data-valmsg-for="firstName"</code> as the key for the localization, that'd return the same error message for all the errors concerning the same property.</p>
  
  <p><strong>To Round it Up</strong></p>
  
  <p>What would be the Best Practise, when <code>ModelState</code> is available, to emit a unique ID for ModelError? Considering I'm using ASP.NET MVC4 and <code>DataAnnotationsModelValidatorProvider</code>.</p>
  
  <p>I can think of tons of ways to "Hack it Together" but I would like to use the <code>ModelState</code> and whatever MVC provides. If it all goes down to writing a custom <code>ModelValidatorProvider</code>, then I'm all open for it. As long as it is the best and most sustainable way of going about it. I'm all for <strong>Doing More Now and Less Later</strong> than <strong>Hacking it Now and Hacking it Forever to Keep It Working</strong></p>