title: MVC 6 Custom Taghelper Validation - Solution
body: <p>Moving from HtmlHelpers I created a few year ago, I have a custom Taghelper that as is marked up during development as so...</p>
  
  <pre><code>    &lt;gdropdown asp-for="Type_ID" asp-items="cboType_SelectOne"&gt;&lt;/gdropdown&gt;
  </code></pre>
  
  <p>All works fine and the output for this is as follows...</p>
  
  <pre><code>    &lt;div&gt;
          &lt;select id="Type_ID" name="Type_ID"&gt;
              &lt;option value=-1&gt; - Select One - &lt;/option&gt;
              &lt;option value=9&gt;Aux. Ext. Device&lt;/option&gt;
              &lt;option value=28&gt;Backup Device&lt;/option&gt;
              ...
          &lt;/select&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Now I am looking for help crafting my OWN validation SPAN to add to the output as follows...</p>
  
  <pre><code>    &lt;div&gt;
          &lt;select id="Type_ID" name="Type_ID"&gt;
              &lt;option value=-1&gt; - Select One - &lt;/option&gt;
              &lt;option value=9&gt;Aux. Ext. Device&lt;/option&gt;
              &lt;option value=28&gt;Backup Device&lt;/option&gt;
              ...
          &lt;/select&gt;
  
          &lt;span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Type_ID"&gt;
              &lt;span for="Type_ID"&gt;Req!&lt;/span&gt;
          &lt;/span&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Anyway, I can manage to do the basics of creating that SPAN, however I can't get to the data annotations for the field that is being added/edited.</p>
  
  <p>Using HtmlHelpers I could use htmlHelper.ValidationMessageFor() as follows in order to craft the output automatically...</p>
  
  <pre><code>    public static MvcHtmlString CustomLookupFor2&lt;TModel, TValue&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper,
          Expression&lt;Func&lt;TModel, TValue&gt;&gt; exp, string url, bool includeValidation, object options)
      {
          ...
          if (includeValidation) sbCtrls.Append(htmlHelper.ValidationMessageFor(expression));
          ...
      }
  </code></pre>
  
  <p>From within the TagHelper ProcessAsync method, how can I get to the same or similar data annotation information? Or better still, how can I auto generate the validation element entirely in in a similar fashion as above?</p>