-1

I want to pass the variable grid to a method(controller)

Here is my ajax code:

function SaveReorderPriority(){

var partnerId = $("#partnerDropDownList").val();
var isoCountryCd = $("#cntryDropDownList").val();
var partnerSystem = $("#partnerSysDropDownList").val();
var grid = $("#critCombiMapGrid").jqGrid('getGridParam', 'data');

if (partnerId != "" && isoCountryCd != "" && partnerSystem!="") {

    $.ajax({
        type: 'POST',
        url: '../../C3Web/CriteriaCombinationMapping/Index',
        dataType: 'json',
        data: { partnerId: partnerId, isoCountryCd: isoCountryCd, partnerSystem: partnerSystem, grid : grid},

Here is my method from Controller. Please help me to pass the variable grid to this method parameter:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(String grid, string partnerId, string isoCountryCd, string partnerSystem)
{
  • `grid` will be an array of complex objects, so for a start you need `data: JSON.stringify({ partnerId: partnerId, .... , grid : grid }),` and add the `contentType: 'application/json',` option. And then `grid` in the POST method needs to be `IEnumerable` where `T` is a model matching the properties your grid is serializing –  Mar 09 '18 at 09:51

1 Answers1

0

var grid = jqGrid('getGridParam', 'data') returns an array

you can pass the array by converting it into string like so:

grid.toString()

data: { grid : grid.toString(), partnerId: partnerId, isoCountryCd: isoCountryCd, partnerSystem: partnerSystem},

and then split the string back to an array on the server:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(String grid, string partnerId, string isoCountryCd, string partnerSystem)
{
   string[] gridData = grid.Split(',');

EDIT

You had grid first and i missed it, so I added it again in my first answer.

You are passing grid last:

data: { partnerId: partnerId, isoCountryCd: isoCountryCd, partnerSystem: partnerSystem, **grid : grid**},

But you have declared it first:

public ActionResult Index(String grid, string partnerId, string isoCountryCd, string partnerSystem)

I would change the order just to avoid any confusion

Ted
  • 3,985
  • 1
  • 20
  • 33