question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How best to pass AdditionalParameters in request?

See original GitHub issue

Great library guys.

I didn’t know where else to ask this, hopefully this is semi-appropriate… I’m trying to send column level custom filter data (date ranges, lookups, etc) back to the server.

I see that IDataTablesRequest can take an AdditionalParameters dictionary, but I’m not sure exactly how to structure my object on the client or where to pass it in. ajax.data perhaps? I’d love to see an example.

Thanks in advance for the help

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
ronnieoverbycommented, Feb 12, 2018

In my aspnet core project, I just did this:

// some.js
$('#myTable').DataTable({
    serverSide: true,
    ajax: {
        url: '/someAjaxUrl',
        data: function (d) {
            d.appJson = JSON.stringify({
                someNumber: 123,
                someString: "hello",
                someNumbers: [1,2,3]
            });
        }
    },
    // other stuff
});
// Startup.cs
services.RegisterDataTables(ctx =>
{
    var appJson = ctx.ValueProvider.GetValue("appJson").FirstValue ?? "{}";
    return JsonConvert.DeserializeObject<IDictionary<string, object>>(appJson);
}, true);


// DataTablesExts.cs
public static class DataTablesExts
{
    public static T Get<T>(this IDataTablesRequest request, string key)
    {
        var obj = request.AdditionalParameters[key];
        return Convert<T>(obj);
    }

    public static bool TryGet<T>(this IDataTablesRequest request, string key, out T value)
    {
        if (request.AdditionalParameters.TryGetValue(key, out object obj))
        {
            value = Convert<T>(obj);
            return true;                
        }

        value = default(T);
        return false;
    }

    private static readonly JToken JNull = JToken.Parse("null");
    private static T Convert<T>(object obj)
    {
        T AsNull() => JNull.ToObject<T>();

        if (obj == null)
            return AsNull();

        if (obj is T tobj)
            return tobj;

        if (obj is JToken jt)
            return jt.ToObject<T>();

        try
        {
            return JToken.FromObject(obj).ToObject<T>();
        }
        catch (FormatException) when (obj is string s && string.IsNullOrWhiteSpace(s))
        {
            return AsNull();
        }
    }
}


// SomeController.cs or SomePage.cshtml.cs
public async Task<DataTablesJsonResult> OnGetQueryAsync(IDataTablesRequest request)
{
    var someNumber = request.Get<int>("someNumber");
    var someString = request.Get<string>("someString");
    var someNumbers = request.Get<int[]>("someNumbers");
    // do other work here....
}

Not the best thing in the world, but damn, I just wanted to get on with building my app.

bitmoji

3reactions
ALMMacommented, Apr 7, 2017

@danielearrighi Thanks!

That sounds interesting. The only downside I can think of if the multiple .Where, which could be replaced by a single (faster compiling/running) statement.

I’ll sure give that a try. Maybe provide an automatic binder (with some automatic request parameters parsing) and a manual binder (with override capability to solve complex cases, like nested objects).

I just need to test, however, on POST. Dunno for sure right now but in some cases, after parsing body element, you cannot read values again. I have to double check that on all AspNet Core, MVC and WebApi to make sure that parsing parameters automatically won’t break the main request params binding (eg: draw, columns, etc).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Correct way to pass multiple values for same parameter ...
I'm looking into what is the correct way to pass multiple values for the same parameter name in a GET request. I've seen...
Read more >
How to Pass Parameters With a GET Request in ASP.NET ...
This article will discuss ways to pass parameters with a GET request in ASP.NET Core, including query params, route params, ...
Read more >
A Beginner's Guide to URL Parameters
URL parameters are query strings that provide additional information and data for a given URL.
Read more >
How to Pass Multiple Parameters GET Method ASP.NET ...
You have multiple options for passing multiple parameters to a GET method: FromRouteAttribute, FromQuery and Model Binding.
Read more >
Best practice for REST API call with many parameters
Best practice is to POST the parameters as an object. This avoids the URL length limit and other problems with query strings.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found