Extending Sanderson's custom mvc ModelBinder for an object stored in session

大兔子大兔子 提交于 2019-12-06 05:26:47

Try inheriting from DefaultModelBinder instead of IModelBinder, then you can call base.BindModel to populate bindingContext.Model for mvc 1.0 or bindingContext.ModelMetadata.Model for mvc 2.0

To trigger bindingContext.Model to populate, call UpdateModel on the controller.

You need to add the statement from the book back in

if(bindingContext.Model != null)
throw new InvalidOperationException("Cannot update instances");

but change it to populate model and save on the session.

if(bindingContext.Model != null)
{
    base.BindModel(controllerContext, bindingContext);
    //save bindingContext.Model to session, overwriting current.
    return bindingContext.Model
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!