title: Duplicate input ids when using multiple partials in ASP.NET MVC
body: <p>I have three partial views that are each strongly typed with separate models.  Each view contains it's own form and submits to different actions.  Some of the models contain properties with the same names and when I use the html helper methods to create the textboxes and labels, I'm ending up with duplicate html ids on the page.</p>
  
  <p><strong>Partial _Residential View</strong></p>
  
  <pre><code>@model MyProject.Models.ResidentialModel
  
  @using (Html.BeginForm("Residential", "Transaction"))
  {
    @Html.LabelFor(m =&gt; m.PersonName)
    @Html.TextBoxFor(m =&gt; m.PersonName)
  
    @Html.LabelFor(m =&gt; m.ReferenceNumber)
    @Html.LabelForm(m =&gt; m.ReferenceNumber)
  
    &lt;input type="submit" value="Submit" /&gt;
  }
  </code></pre>
  
  <p><strong>Partial _Business View</strong></p>
  
  <pre><code>@model MyProject.Models.BusinessModel
  
  @using (Html.BeginForm("Business", "Transaction"))
  {
    @Html.LabelFor(m =&gt; m.BusinessName)
    @Html.TextBoxFor(m =&gt; m.BusinessName)
  
    @Html.LabelFor(m =&gt; m.ReferenceNumber)
    @Html.LabelForm(m =&gt; m.ReferenceNumber)
  
    &lt;input type="submit" value="Submit" /&gt;
  }
  </code></pre>
  
  <p><strong>Normal View</strong></p>
  
  <pre><code>&lt;h2&gt;Residential Transaction&lt;/h2&gt;
  @Html.Partial("_Residential")
  
  &lt;h2&gt;Business Transaction&lt;/h2&gt;
  @Html.Partial("_Business")
  </code></pre>
  
  <p>The output that I'm getting looks like the following:</p>
  
  <pre><code>&lt;h2&gt;Residential Transaction&lt;/h2&gt;
  &lt;form action="/Transaction/Residential" method="post"&gt;
    &lt;label for="PersonName"&gt;Person Name:&lt;/label&gt;
    &lt;input type="text id="PersonName" name="PersonName" /&gt;
  
    &lt;label for="ReferenceNumber"&gt;Reference Number:&lt;/label&gt;
    &lt;input type="text" id="ReferenceNumber" name="ReferenceNumber" /&gt;
  
    &lt;input type="submit" value="Submit" /&gt;
  &lt;/form&gt;
  
  &lt;h2&gt;Business Transaction&lt;/h2&gt;
  &lt;form action="/Transaction/Business" method="post"&gt;
    &lt;label for="BusinessName"&gt;Business Name:&lt;/label&gt;
    &lt;input type="text" id="BusinessName" name="BusinessName" /&gt;
  
    &lt;label for="ReferenceNumber"&gt;Reference Number:&lt;/label&gt;
    &lt;input type="text" id="ReferenceNumber" name="ReferenceNumber" /&gt;
  
    &lt;input type="submit" value="Submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Since ReferenceNumber is in both models, I'm getting a duplicate id on the page.  I figured out that i can pass an extra htmlAttributes parameter to TextBoxFor to change the id.</p>
  
  <pre><code>@Html.TextBoxFor(m =&gt; m.ReferenceNumber, new { id = "ResidentialReferenceNumber" })
  ...
  @Html.TextBoxFor(m =&gt; m.ReferenceNumber, new { id = "BusinessReferenceNumber" })
  </code></pre>
  
  <p>This fixes the ids on the inputs but the 'for' attribute on the labels still has the wrong value.  I looked through the various overloads for LabelFor() and unlike the TextBoxFor, there isn't an htmlAttributes property.  Is there some way that I can tell model or view to use a different id for these fields?</p>