When an Action
gets called the framework builds a ModelStateCollection
based on the query-string values, post-data, routing values etc. And this ModelStateCollection
will be passed to the View
. All the HTML input helpers try to the get the values from the ModelStateCollection
first, before trying to get the values from the actual model.
Because your input model is the int id
but the output model is some new model the helpers will use the values from the ModelStateCollection
(from the query string) because the propery names Id
are match.
To make it work you have to manually clear the ModelStateCollection
before returning the new model to the view:
public ActionResult SomeAction(int Id){
//Id is set to 2
ModelState.Clear();
var model = //get some thing from db using Id(2);
//Now model.Id is set to 9;
return View(model);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…