Is it safe to blindly overwrite the ModelMetaData of a binding context to set the type of the model? I’m worried the binder may be used to update pre-built models in edit scenarios and I will lose data if I just overwrite the ModelMetaData.
Sample code:
Public Class CustomModelBinder
Inherits DefaultModelBinder
Public Overrides Function BindModel(ByVal controllerContext As ControllerContext, ByVal bindingContext As ModelBindingContext) As Object
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(Nothing, GetSpecificModelTypeBasedOnBindingData(bindingContext))
Return MyBase.BindModel(controllerContext, bindingContext)
End Function
End Class
Yes, it is possible that the model will be already specified. This occurs, for example, when someone tries to update an existing model using
TryUpdateModel/UpdateModel, as shown here.