Question

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]"];
Was it helpful?

Solution

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());

OTHER TIPS

For asp.net core I didn't use a model binder, as the data is sent as a Dictionary I just used the following method signature on my api and the binding happened automatically (no parameter mapping required client side)

public async Task<JsonResult> GetAccessions(.., IDictionary<string, string>[] sort)
{
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top