I have the following view model:
public class FormViewModel {
[Required, StringLength(100)]
public string Name { get; set; }
private object _parameters = null;
public object Parameters {
get {
if (_parameters == null)
_parameters = Activator.CreateInstance(Type.GetType("CustomParameters"));
return _parameters;
}
set {
_parameters = value;
}
}
}
Where CustomParameters looks like:
public class CustomParameters {
[Required]
public string Text { get; set; }
}
Now If I post the following form data:
"Name" => "Foo"
"Parameters.Text" => "Bar"
The "Name" property is correctly set, however the "Parameters.Text" property is set to null.
Please note that the above scenario has been simplified and the Parameters needs to support binding to multiple custom types.
Edit - I've added the following code I used in ASP.NET MVC but ASP.NET Core's model binding looks to have been rewritten and I can't see what I need to do:
public class IRuntimeBindableModelBinder : DefaultModelBinder {
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
var newBindingContext = new ModelBindingContext() {
// In the original method you have:
// ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => bindingContext.Model, typeof(TModel)),
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => bindingContext.Model, bindingContext.Model.GetType()),
ModelName = bindingContext.ModelName,
ModelState = bindingContext.ModelState,
PropertyFilter = bindingContext.PropertyFilter,
ValueProvider = bindingContext.ValueProvider
};
return base.BindModel(controllerContext, newBindingContext);
}
}
I'd appreciate it if someone could help.
Thanks
CustomParametersgeneric, or you don't know the incoming fields beforehand?