Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

TryUpdateModel: Determine model type at runtime #1837

@maxtoroq

Description

@maxtoroq

The TryUpdateModel methods require that you explicitly declare the type you are binding at compile time, and if you attempt to bind a subclass instance the subclass properties are ignored. This is because the model metadata is requested using the type parameter:

var modelMetadata = metadataProvider.GetMetadataForType(
    modelAccessor: null,
    modelType: typeof(TModel));

...instead of using the type of the model instance:

var modelMetadata = metadataProvider.GetMetadataForType(
    modelAccessor: null,
    modelType: model.GetType());

Even with this small change you are still forced to provide the TModel type parameter, but at least you can pass a base type for TModel and have a derived instance be properly updated.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions