Controller action method only for the first time. Why doesn't he get hit again?

When I click the FilterButton button, it accesses a warning every time after every click. However, the GridData action method only hits the first time. Do you know, why?

$(document).ready(function () { $('#FilterButton').click(function () { alert('button clicked'); var myGrid = jQuery("#list").jqGrid({ url: '/Data/GridData/', datatype: 'json', mtype: 'POST', ...... 

Here is the requested action method

  [HttpPost] public JsonResult GridData(string sidx, string sord, int page, int rows, string species, string methodOfTake, string outputType, string seasons, string years) { var results = (from a in db.t_harvest_statistics_elk where a.year == "2008" && a.unit_number == 1 orderby a.id select new { a.id, a.year, a.unit_number, a.total_hunters, a.bulls, a.cows }).ToList(); int pageIndex = Convert.ToInt32(page) - 1; int pageSize = rows; int totalRecords = results.Count(); int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize); var pageResults = results.Skip(pageIndex * pageSize).Take(pageSize); var jsonData = new { total = totalPages, page, records = totalRecords, rows = ( from pageResult in pageResults select new { id = pageResult.id, cell = new[] { pageResult.year.ToString(), "Add", pageResult.unit_number.ToString(), pageResult.total_hunters.ToString(), pageResult.bulls.ToString(), "add", pageResult.cows.ToString(), "add", "add", "add" } }).ToArray() }; return Json(jsonData, JsonRequestBehavior.AllowGet); } 
+4
source share
1 answer

The jqGrid control is an interesting beast.

What happens here is that you initialize the grid for the first call, and therefore setting up the grid for the first time will initialize the data needed for the grid. This initialization triggers the controller and causes it to hit the breakpoint for the first time.

For each subsequent click that happens, you call the jqGrid () method, but since it is already initialized, it ignores the call and therefore does not set the parameters that you specify.

Instead, you need to call the setGridParam method, which will allow you to update the jqGrid parameters as follows:

 jQuery('#list').setGridParam({ url: '/Data/GridData/', datatype: 'json', mtype: 'POST', ... ); 

after changing the grid parameters you need to call the update in this way:

 jQuery('#list').trigger("reloadGrid"); 

This is not the most intuitive setting, but this is how jqGrid works.

+1
source

Source: https://habr.com/ru/post/1400916/


All Articles