This is a migrated thread and some comments may be shown as answers.

[Solved] onDataBinding post data not sent to action on AJAX().Select

4 Answers 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nathan
Top achievements
Rank 1
Nathan asked on 22 Dec 2011, 05:25 AM
Hello,

I have a custom filtering solution I've built for the MVC Grid, which works wonderfully, except for the fact that I can't get the filter data added into the post when using AJAX().Select binding.  

Here is the JavaScript I registered with onDataBinding. 
function dataBinding(args)
 {
     var filter = "";
     var filterableColumns = $("[prism-name^=filter-column]");
     for(var i = 0; i < filterableColumns.length; i++)
     {
         if(filterableColumns[i].value != "")
         {
             if(filter != "") { filter += "~"; }
             filter += filterableColumns[i].getAttribute('prism-name').replace("filter-column-", "") + "|" + filterableColumns[i].value;
         }
         document.cookie = createCookie(window.location.pathname + "/" + filterableColumns[i].getAttribute('prism-name'), filterableColumns[i].value);
     }
     args.data = filter;       
 }

I've debugged this code and args.data is being set to filter, but for some reason, that data isn't added to the post (I verified this with Fiddler) and is therefore not available to my action.

Here's my controller code.

[GridAction]
 public ActionResult _Filter(string filter)
 {
     if (filter == null)
     {
         filter = string.Empty;
     }
 
     var project = filter.DeserializeFilterString<Project>();
 
     return View(new GridModel<Project>(db.Projects.Where(p => (string.IsNullOrEmpty(project.ProjectType) || p.ProjectType.Contains(project.ProjectType))).ToList()));
 }

The string "filter" in this action is always null.  I've tested that the action works by mocking up a post request using Fiddler where I added the proper filter string and it's captured just fine by the action. 

Thanks!

Nathan

4 Answers, 1 is accepted

Sort by
0
Accepted
Petur Subev
Telerik team
answered on 22 Dec 2011, 04:41 PM
Hello Nathan,

To correctly pass the filter descriptor use a different parameter name than filter and also add the filter descriptor so it extend the data property.
e.g.

args.data = {filterDescr : yourFilterAsString}

Also do not forget to change the name of  the parameter to filterDescr in the controller.

Regards,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Nicolás
Top achievements
Rank 1
answered on 28 Dec 2011, 03:29 PM
Hello,

I have the same problem:

@{Html.Telerik().Grid(Model)
      .Name("Grid")
      .Columns(columns =>
      {
          columns.Bound(v => v.AgentFullName).Title("Agente");
          columns.Bound(v => v.Date).Format("{0:d}").Title("Fecha");
          columns.Bound(v => v.Planned).Title("Planeadas");
          columns.Bound(v => v.Effective).Title("Efectivas");
          columns.Bound(v => v.PreInscript).Title("Preinscritas");
          columns.Bound(v => v.MissingDocs).Title("Faltan Documentos");
          columns.Bound(v => v.PreInscript).Title("No Efectivas");
      })
      .DataBinding(dataBinding => dataBinding
          .Ajax()
          .Select("_Details", "Visits")
      )
      .ClientEvents(events => events.OnDataBinding("Grid_DataBinding"))
      .Render();
}

function Grid_DataBinding(args) {
    var from = $("#from").data("tDatePicker").value();
    var to = $("#to").data("tDatePicker").value();
 
    args.data = { from: from, to: to };
}

[GridAction]
public ActionResult _Details(DateTime? from, DateTime? to)
{
    return View(new GridModel(GetVisits(from, to)));
}

Thanks for your help!!
0
Nicolás
Top achievements
Rank 1
answered on 29 Dec 2011, 03:49 PM
I've found the solution

function Grid_DataBinding(args) {
        var from = $("#from").data("tDatePicker").value();
        var to = $("#to").data("tDatePicker").value();
 
        var fromFormatted = $.telerik.formatString('{0:G}', from);
        var toFormatted = $.telerik.formatString('{0:G}', to);
 
        args.data = $.extend(args.data, { from: fromFormatted, to: toFormatted });
    }

I have to format the date, I think this is a bug.

Thanks!!
0
Nathan
Top achievements
Rank 1
answered on 03 Jan 2012, 06:04 PM
Perfect!  It was exactly like you said.  Once I started using a different property name other than "filter" it started working as expected.  
Tags
Grid
Asked by
Nathan
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Nicolás
Top achievements
Rank 1
Nathan
Top achievements
Rank 1
Share this question
or