title: ASP.NET MVC 3 Model Id using Route Id value
body: <p><strong>Scenario</strong><br>
  Route: /template/customize/10
  Where: 10 = ID of Template()</p>
  
  <p>In the controller the model is created based on the template so that the View's model is actually a Customization() object which actually has an Id of 0 because it's new.</p>
  
  <p>In the view I render @Html.HiddenFor( m => m.Id ) and the resulting value of that hidden input is <strong>10</strong>, though it should be <strong>0</strong> because m is of type Customization.  I've run into this before with MVC 2 and worked around it by not using helper methods.  </p>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li><p>Is there annotation or something I
  can add to the Html Helper method to
  actually render the correct value?  </p></li>
  <li><p>Is this a bug (MVC seems to be
  rendering m.Id as the route value
  regardless of what the actual model
  is set to in the controller)?</p></li>
  </ol>
  
  <p><strong>Additional code for clarification</strong></p>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model Project.Core.Domain.Customization
  @using( Html.BeginForm( "save", "customization" ) )
  {
      @Html.HiddenFor( m =&gt; m.Id )
      @Html.HiddenFor( m =&gt; m.Template.Id )
      &lt;button type="submit" id="save" name="save"&gt;Save&lt;/button&gt;
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>       public ActionResult Customize( int id )
      {
          var template = Persistence.Data.RetrieveObject&lt;Template&gt;( id );
          var model = new Customization();
  
          ViewBag.Template = template;
          return ( View( model ) );
      }
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <p>Changed signature of Action to:</p>
  
  <pre><code>public ActionResult Customize( int TemplateId ){ ... }
  </code></pre>
  
  <p>Changed link to action as such:</p>
  
  <pre><code>@Html.ActionLink( "customize", "customize", new { TemplateId = template.Id } )
  </code></pre>
  
  <p>I end up with a url that looks like</p>
  
  <pre><code>/template/customize?TemplateId=10
  </code></pre>
  
  <p>It's uglier, but I get to keep my view clean with model.Id so this is a win for me.</p>