How to convert QueryString to a Model with Mvc4 not via controller

548 views Asked by At

I would like to imitate after the controller binding process and bind the query string into a model but not via controller.

I have access to Request.QueryString from type NameValueCollection. How can i force it to get bind the same as mvc4 binding the models via the controller.

For example i have this class:

public class Example
{
   public string Name {get;set;}
   public string LastName {get;set;}
}

and NameValueCollection(Request.QueryString) that created by the Request object from the url that is look like ?Name=James&Lastname=Bow.

Any suggestions?

1

There are 1 answers

3
Spock On

Why not create an ActionFilter then override the OnActionExecuted method. ActionExecutedContext would have access to Request.QueryString. You can then populate the filterContext.Controller.ViewData.Model from the query string.