What is the difference between BindProperty and SetProperty on IModelBinder

偶尔善良 提交于 2019-12-03 11:42:38

First of all, BindProperty is not a part of IModelBinder but, a protected method in DefaultModelBinder. You can access it only if you are sub-classing the DefaultModelBinder.

The following points should answer your question:

  • BindProperty uses the IModelBinder interface it gets from the PropertyType of the propertyDescriptor argument. This allows you to inject custom properties into the property metadata.
  • BindProperty properly handles validation. It (also) calls the SetProperty method only if the new value is valid.

So if you want proper validation (using the annotation attributes) you must definitely call BindProperty. By calling SetProperty you bypass all the built-in validation mechanisms.

You should check out the source code of DefaultModelBinder the see what each method does, since the intellisense provides only limited information.

I think SetProperty takes actual value to set as the last parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!