ALMMa / datatables.aspnet

Microsoft AspNet bindings and automatic parsing for jQuery DataTables along with extension methods to help on data queries.
MIT License
303 stars 136 forks source link

AdditionalParameters ignored #41

Closed sepptruetsch closed 7 years ago

sepptruetsch commented 8 years ago

Hi

I'm trying to pass some additional filter values to the server side processing. But what ever I try the AdditionalParameters property is always null. I've seen issue #19 and #20 but enable and register this in Global.asax. I'm using the latest DataTables.AspNet.WebApi2 v 2.0.0 NuGet.

This is my global.asax registration:

        protected void Application_Start()
        {
            Func<System.Web.Http.Controllers.HttpActionContext, System.Web.Http.ModelBinding.ModelBindingContext, IDictionary<string, object>> parser = (httpContext, modelContext) =>
            {
               // never gets called
                return new Dictionary<string, object>();
            };
            var options = new DataTables.AspNet.WebApi2.Options();
           // should this be enough?
            options.EnableRequestAdditionalParameters();
            // desperate
            options.EnableResponseAdditionalParameters();
                        DataTables.AspNet.WebApi2.Configuration.RegisterDataTables(GlobalConfiguration.Configuration, options, new DataTables.AspNet.WebApi2.ModelBinder() { ParseAdditionalParameters = parser });

             // ... all other register calls afterwards
        }

On the client side I'm adding my values:

data: function(data) { data.ExtraFilter = { Customer: true, Projects: true }; }

The values are transmitted (Chrome Network tab) and with my custom model they also get deserialized/bound. But I really would like to use the interface from the nuget instead rebuilding everything.

I don't know what I'm doing wrong as the examples are rather simple and don't address the AdditionalParameters property. As my parser never gets called even tough the option is enabled something else must be missing?!

Thanks!

mojohub commented 8 years ago

I am also stuck with this same issue of AdditionalParamters = null using ASP.NET Core. So i tried setting it up as per #19 but I don't know where the custom parser function needs to go. I've created a separate helper class with this function and passing it as a parameter when registering the service but it's not finding it so clearly my setup is wrong somewhere. It would be good to see this feature used in the provided examples since it appears more complex than anticipated.

Also should we be creating a separate parser function for each datatable we use? I was hoping that registering the service with parameters enabled would be a generic one-off approach for any datatable we have in our application.

Here is my helper function...

    public class DataTablesParameterParser
    {

        // Parses custom parameters sent with the request.
        public IDictionary<string, object> DataTablesParser(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var values = new Dictionary<string, object>();

            var sampleParameter = bindingContext.ValueProvider.GetValue("p1");
            values.Add("p1", sampleParameter.FirstValue);

            return values;
        }

    }

This is how i'm registering the service in my Startup.cs file... services.RegisterDataTables(DataTablesParser, false);

My app won't compile due to error in startup file...

The name DataTablesParser does not exist in the current context

sepptruetsch commented 7 years ago

@mojohub As far as I understood the parser needs to be passed into the configuration and/or the ModelBinder

I can reproduce the same behaviour in the sample project:

After enabling EnableRequestAdditionalParameters and passing in a ModelBinder with parser the additional fields still wont get parsed.

So what is missing?

ALMMa commented 7 years ago

Custom parser should be passed no registration. So, for instance:

services.RegisterDataTables(_parser, false);

@sepptruetsch looking into that. No clue yet, so it might take a while. Function is set and parsing enabled, but won't take place. Something might be interfering with the runtime variables or instances and I'm looking into that.

@mojohub your code won't compile because you're incorrectly referencing your method. That's why you're getting a compile error. You have to pass the function as a parameter. Best approach based on your case would be:

services.RegisterDataTables(new DataTablesParameterParser().DataTablesParser);

Please remember that you function signature is incorrect too. You're function must receive one parameter of type ModelBindingContext and return an IDictionary<string, object>.

ALMMa commented 7 years ago

@sepptruetsch It's was due to an internal error on the registration class.

When using the full registration (which is used to support additional parameters) the model binder get's incorrectly recreated when populating the binders collection.

I'm releasing an update (2.0.1) to fix this issue. Please, update your references and check if it's fixed.