public ActionResult SomeAction(int Id){
//Id is set to 2
var model = //get some thing from db using Id(2);
//Now model.Id is set to 9;
return View(model);
}
----------View----------
@Html.HiddenFor(x => x.Id)
When I view source this hidden field is set to 2 not 9. How do I get it to map to the model instead of mapping to the URL routing info?
P.S. I’d prefer not to rename parameters because then I lose my nice looking url’s unless i change the routing info. I have done that and it does work, but not what I want.
When an
Actiongets called the framework builds aModelStateCollectionbased on the query-string values, post-data, routing values etc. And thisModelStateCollectionwill be passed to theView. All the HTML input helpers try to the get the values from theModelStateCollectionfirst, before trying to get the values from the actual model.Because your input model is the
int idbut the output model is some new model the helpers will use the values from theModelStateCollection(from the query string) because the propery namesIdare match.To make it work you have to manually clear the
ModelStateCollectionbefore returning the new model to the view: