Scenario
Route: / template / customize / 10 Where: 10 = template identifier ()
In the controller, the model is created based on the template, so the View model is actually a Customization () object, which actually has the identifier 0, because it is new.
In the view, I render @ Html.HiddenFor (m => m.Id), and the resulting value for this hidden input is 10 , although it should be 0 because m is of type Customization. I came across this before with MVC 2 and worked around it without using helper methods.
Questions
Is there an annotation or something else that can add the Html Helper method to really make the right value?
Is this a mistake (it seems MVC rendering m.Id as a route value no matter what actual model is installed in the controller)?
Additional code for clarification
View
@model Project.Core.Domain.Customization @using( Html.BeginForm( "save", "customization" ) ) { @Html.HiddenFor( m => m.Id ) @Html.HiddenFor( m => m.Template.Id ) <button type="submit" id="save" name="save">Save</button> }
controller
public ActionResult Customize( int id ) { var template = Persistence.Data.RetrieveObject<Template>( id ); var model = new Customization(); ViewBag.Template = template; return ( View( model ) ); }
Decision
Action signature changed:
public ActionResult Customize( int TemplateId ){ ... }
Changed the link to the action as such:
@Html.ActionLink( "customize", "customize", new { TemplateId = template.Id } )
I get a url that looks like
/template/customize?TemplateId=10
It's uglier, but I get my view to be clear with the model. So this is a victory for me.