title: ModelMetadata TemplateHint always null
body: <p>I am trying to create a custom <code>ModelMetadataProvider</code> to provide unobtrusive attributes for the JQuery UI Autocomplete widget.</p>
  
  <p>I have a custom attribute that looks like this:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
  public sealed class AutocompleteAttribute : Attribute, IMetadataAware
  {
      public void OnMetadataCreated(ModelMetadata metadata)
      {
          metadata.TemplateHint = "Autocomplete";
      }
  }
  </code></pre>
  
  <p>and an editor template that looks like this:</p>
  
  <pre><code>@{
      var attributes = new RouteValueDictionary
      {
          {"class", "text-box single-line"},
          {"autocomplete", "off"},
          {"data-autocomplete-url", "UrlPlaceholder" },
      };
  }
  
  @Html.TextBox("", ViewContext.ViewData.TemplateInfo.FormattedModelValue, attributes)
  </code></pre>
  
  <p>I have a viewModel with a property of type <code>string</code> that includes the <code>AutocompleteAttribute</code> like this:</p>
  
  <pre><code>public class MyViewModel
  {
      [Autocomplete]
      public string MyProperty { get; set; }
  }
  </code></pre>
  
  <p>When I use this viewModel in my view I check the generated html and I am getting an <code>&lt;input&gt;</code> tag which has an attribute like this: <code>data-autocomplete-url="UrlPlaceholder"</code>.</p>
  
  <p>What I want to do next is to be able to specify the URL in my view that uses my viewModel like this:</p>
  
  <pre><code>@model MyViewModel
  
  @{ ViewBag.Title = "Create item"; }
  
  @Html.AutoCompleteUrlFor(p =&gt; p.MyProperty, UrlHelper.GenerateUrl(null, "Autocomplete", "Home", null, Html.RouteCollection, Html.ViewContext.RequestContext, true))
  
  // Other stuff here...
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>My <code>AutoCompleteForUrl</code> helper just saves the generated URL in a dictionary, using the property name as a key.</p>
  
  <p>Next I have created a custom <code>ModelMetadataProvider</code> and registered it in global.asax using this line of code <code>ModelMetadataProviders.Current = new CustomModelMetadataProvider();</code>.</p>
  
  <p>What I want to do is to insert the URL to be used by the JQuery UI Autocomplete widget into the <code>metadata.AdditionalValues</code> dictionary to be consumed by the Autocomplete editor template.</p>
  
  <p>My custom <code>ModelMetadataProvider</code> looks like this:</p>
  
  <pre><code>public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
  {
      protected override ModelMetadata CreateMetadata(IEnumerable&lt;System.Attribute&gt; attributes, Type containerType, Func&lt;object&gt; modelAccessor, Type modelType, string propertyName)
      {
          var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
  
          if (metadata.TemplateHint == "Autocomplete")
          {
              string url;
              if(htmlHelpers.AutocompleteUrls.TryGetValue(metadata.propertyName, out url)
              {
                  metadata.AdditionalValues["AutocompleteUrl"] = url;
              }
          }
  
          return metadata;
      }
  }
  </code></pre>
  
  <p>and my updated editor template looks like this:</p>
  
  <pre><code>@{
      object url;
  
      if (!ViewContext.ViewData.ModelMetadata.TryGetValue("AutocompleteUrl", out url))
      {
          url = "";
      }
  
      var attributes = new RouteValueDictionary
      {
          {"class", "text-box single-line"},
          {"autocomplete", "off"},
          {"data-autocomplete-url", (string)url },
      };
  }
  
  @Html.TextBox("", ViewContext.ViewData.TemplateInfo.FormattedModelValue, attributes)
  </code></pre>
  
  <p>The problem is, the <code>TemplateHint</code> property never equals "Autocomplete" in my custom model metadata provider so my logic to generate the URL never gets called. I would have thought that at this point the <code>TemplateHint</code> property would be set as I have called the base implementation of <code>CreateMetadata</code> of the <code>DataAnnotationsModelMetadataProvider</code>.</p>
  
  <p>Here's what I can confirm:</p>
  
  <ul>
  <li>The <code>CustomModelMetadataProvider</code> is correctly registered as it contains other code which is getting called.</li>
  <li>The correct editor template is getting picked up as the Html that is generated contains an attribute called <code>"data-autocomplete-url"</code>.</li>
  <li>If I put a breakpoint in the Autocomplete template, Visual Studio goes to the debugger.</li>
  </ul>
  
  <p>So can anyone shed any light on this for me please? What am I misunderstanding about the <code>ModelMetadataProvider</code> system?</p>