,如果你想改變這種默認行爲,您可能需要自定義模型粘合劑:
public class MyModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
// TODO: decide if you need to instantiate or not the model
// based on the context
if (!ShouldInstantiateModel)
{
return null;
}
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
然後:
public ActionResult Index([ModelBinder(typeof(MyModelBinder))] Home document)
{
return View(new BaseViewModel<Home>(document, _repository));
}
或Application_Start
全球範圍內進行註冊:
ModelBinders.Binders.Add(typeof(Home), new MyModelBinder());
再次感謝 – Marcus 2011-02-05 17:26:43