Afterwards Model Binding in ASP.NET MVC: How to convert QueryString values into a view model?

[亡魂溺海] 提交于 2019-12-25 17:34:48

问题


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.


回答1:


What you're asking for is serialization. For doing this simply, you could put a constructor overload that accepts a QueryStringValueProvider as an argument and that constructor is responsible initializing all of the model's properties based on the provider. If you stick to strings, you could very easily put such a constructor into a model base class that could be inherited by all of your models.

This could also be built into an extension method so it could be called "on demand" rather than at construction.




回答2:


To manually do custom model binding, create a custom model binder (implement IModelBinder) and register it with your IoC container.

Or you could call this.UpdateModel on inside your action method. This should bind the values from your ValueProvider (RouteData, Request.Form collection and QueryString) to your model.




回答3:


You could use TryUpdateModel

public ContentResult TestAction()
{
   var model = new MyModel();

   if(TryUpdateModel(model, new QueryStringValueProvider(ControllerContext)))
   {
      return Content("success");
   }

   return Content("failed");
}



回答4:


My Controller Action

var viewModelTypeName = queryString[ValidationKeys.ViewModelType];
var viewModelType = Type.GetType(viewModelTypeName);
var instance = Activator.CreateInstance(viewModelType);
UpdateModelUsingQueryString(instance);

UpdateModel

protected internal void UpdateModelUsingQueryString<TModel>(TModel model) where TModel : class
{
    if (model == null) throw new ArgumentNullException("model");

    Predicate<string> propertyFilter = propertyName => new BindAttribute().IsPropertyAllowed(propertyName);
    var binder = Binders.GetBinder(typeof(TModel));

    var bindingContext = new ModelBindingContext()
    {
        ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType()),
        ModelState = ModelState,
        PropertyFilter = propertyFilter,
        ValueProvider = new QueryStringValueProvider(ControllerContext)
    };
    binder.BindModel(ControllerContext, bindingContext);
}

The problem was that UpdateModel or TryUpdateModel does not work for object by design. Both methods use typeof(TModel). But you have to use model.GetType().

Take a look at: Model Binding - Type in External Assembly

Darin Dimitrov gave the right answer :)



来源:https://stackoverflow.com/questions/11141613/afterwards-model-binding-in-asp-net-mvc-how-to-convert-querystring-values-into

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