Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind a QueryString that has arrays with MVC

I am using the Telerk Kendo UI grid that loads data remotely. The QueryString passed into my action method looks like this:-

take=10&skip=0&page=1&pageSize=10&sort[0][field]=value&sort[0][dir]=asc

I am trying to work out how to bind the sort parameter into my method? Is it possible or do I need to enumerate through the QueryString collection manually or create a custom binder?

So far I have tried this:-

public JsonResult GetAllContent(int page, int take, int pageSize, string[] sort)

public JsonResult GetAllContent(int page, int take, int pageSize, string sort)

but sort is always null. Does anyone know how I can achieve this?

I can fall back to Request.QueryString using, but this is a bit of a kludge.

var field = Request.QueryString["sort[0][field]"];
var dir = Request.QueryString["sort[0][dir]"];
like image 450
Rippo Avatar asked Mar 04 '12 18:03

Rippo


1 Answers

You could use an array of dictionaries:

public ActionResult Index(
    int page, int take, int pageSize, IDictionary<string, string>[] sort
)
{
    sort[0]["field"] will equal "value"
    sort[0]["dir"] will equal "asc"
    ...
}

and then define a custom model binder:

public class SortViewModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var modelName = bindingContext.ModelName;
        var keys = controllerContext
            .HttpContext
            .Request
            .Params
            .Keys
            .OfType<string>()
            .Where(key => key.StartsWith(modelName));

        var result = new Dictionary<string, string>();
        foreach (var key in keys)
        {
            var val = bindingContext.ValueProvider.GetValue(key);
            result[key.Replace(modelName, "").Replace("[", "").Replace("]", "")] = val.AttemptedValue;
        }

        return result;
    }
}

which will be registered in Global.asax:

ModelBinders.Binders.Add(typeof(IDictionary<string, string>), new SortViewModelBinder());
like image 62
Darin Dimitrov Avatar answered Sep 25 '22 10:09

Darin Dimitrov