Search code examples
asp.net-mvcjqgrid

returning json data from controller


My Colmodel in javascript looks like this

jQuery("#testGrid").jqGrid(

//Some code

colModel: [
 { name: 'field1',index: 'field1', width: 113, align: 'Center', formatter: selectCheckboxFormatter, sortable: false },
{ name: 'field2', index: 'field2', width: 113, align: 'Center' },
{ name: 'field3', index: 'field3', width: 120, align: 'left' }
];

)

and my javascript for datasource look as follows. I call this function at some point in my javascript to populate the grid.

function PopulateDummyData() {
var mydata = [
{ field1: "Yes", field2: "1", field3: "54555464"},
{ field1: "No", field2: "2", field3: "54555464"},
];
}

but I want to get the data in the above function from the controller code, such that the controller action returns a JSON data in the above format, which I can use to populate my grid. and the controller code will be invoked by the grid's URL action using the following code snippet in javascript.

url: UrlAction('MyController', 'PopulateDummyData').

But I am not sure about how the controller code should be? any thoughts or comments??


Solution

  • From some blog:

    public JsonResult GetStateList() {
      var list = new List<ListItem>() {
        new ListItem() { Value = "1", Text = "VA" },
        new ListItem() { Value = "2", Text = "MD" },
        new ListItem() { Value = "3", Text = "DC" }
      };
      return this.Json(list);
    }