I have an action method without parameters.
The QueryString collection contain all of my values. The keys of the QueryString match my view model properties.
var queryStringValueProvider = new QueryStringValueProvider(ControllerContext);
var providerResult = queryStringValueProvider.GetValue(ValidationKeys.Id); // ?!
var viewModelTypeName = queryString[ValidationKeys.ViewModelType];
var viewModelType = Type.GetType(viewModelTypeName);
var viewModelInstance = providerResult.ConvertTo(viewModelType); // throws an InvalidOperationException
How can I convert the QueryString collection to a view model?
ASP.NET MVC already do this when you just pass the view model into the action method parameters. So what I need is an afterwards model binding using ASP.NET MVC mechanics.
My Controller Action
UpdateModel
The problem was that
UpdateModelorTryUpdateModeldoes not work forobjectby design.Both methods use
typeof(TModel). But you have to usemodel.GetType().Take a look at: Model Binding – Type in External Assembly
Darin Dimitrov gave the right answer 🙂