title: ID generation for HTML elements in ASP.NET MVC
body: <p>I need to generate unique identifiers for html elements in asp.net mvc application. 
  In classic asp.net i could use </p>
  
  <pre><code>&lt;a id=&lt;%=ClientID&gt;%&gt;
  </code></pre>
  
  <p>Is there some analog in asp.net mvc world ?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>For example, I want to make a reusable Button element. I would perfer code to look similar to </p>
  
  <pre><code>&lt;div class="myButton" id="&lt;%=ClientID%&gt;"&gt;
  &lt;script&gt;
    var button = document.getElementById(&lt;%=ClientID%&gt;);
    button.onclick = ....
  &lt;/script&gt;
  </code></pre>
  
  <p>If ClientId is not available then what is the best way to follow ?
  For now, I see two options - to generate it like <strong>Guid.NewGuid()</strong> or pass id from the outside ? Any other options ?</p>
  
  <p><strong>UPDATE:</strong>
  For now, I've come to following solution</p>
  
  <pre><code>    public static string UniqueId(this HtmlHelper html)
      {
          var idGenerator = html.ViewContext.HttpContext.Items[typeof (UniqueIdGenerator)] as UniqueIdGenerator;
          if (idGenerator==null)
              html.ViewContext.HttpContext.Items[typeof (UniqueIdGenerator)] = idGenerator = new UniqueIdGenerator();
          return idGenerator.Next();
      }
         ...
      private class UniqueIdGenerator
      {
          private int id;
  
          public string Next()
          {
              id++;
              return "_c" + id; // todo: optimize
          }
      }
  </code></pre>