-
Notifications
You must be signed in to change notification settings - Fork 134
How best to pass AdditionalParameters in request? #50
Comments
It should be noted that I've since found the Mvc5.AdditionalParameters project that was created last week, and have added the |
Check out issue #47 As I've said there, this behavior is not optimal and will be improved but for now, you must enable the request parameters on the DataTables.AspNet options and provide a model binder which will parse your parameters. It's not sufficient to simply send those parameters (at least, for now). I'll implement a automatic bindind but it's important to consider that some complex members might still require a custom binder nonetheless. |
Thanks! Excuse my ignorance, but I'm not sure what you mean by "enable the request parameters on the DataTables.AspNet options". Thanks in advance for your time |
Check code and comments on See more here:
|
Thanks! |
Hello, @spencerflagg Yes, you are correct. You might want to check more about This behavior is due to the very nature of the binding mechanism from each platform. Parser function will always return an |
Hello, what do you think about this generic method to parse the additional parameter?
Basically, it filters all the "standard" querystrings passed by datatables itself, and parses out the remaining parameters in the dictionary. This way I can pass every parameters I want (should not start with columns, order, search of course) and I get it in the dictionary. Like:
What do you think? Any downsides using this approach? Thanks |
@danielearrighi Thanks! That sounds interesting. The only downside I can think of if the multiple 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 |
Hello, I want to return additional parameters from my controllers. Sorry for my bad english. |
Since I'm using POST to send data using DataTables, I modified @danielearrighi solution to something a little bit better:
IE: When HttpMethod is POST I grab Request.Form, else Request.QueryString. The code isn't pretty, I know, but it works! |
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. |
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 anAdditionalParameters
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
The text was updated successfully, but these errors were encountered: