Search code examples
c#asp.net-mvcextjsextjs4extjs4.1

How to get values from grid to c# controller


I am trying to get values from a grid in my c# controller. This is my store and grid

Ext.create('Ext.data.Store', {
    storeId:'store',
    fields:['name', 'email', 'phone'],
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            root: 'items'
        }
     }
});

Ext.create('Ext.grid.Panel', {
    store: Ext.data.StoreManager.lookup('store'),
    columns: [
        { text: 'Name',  dataIndex: 'name' },
        { text: 'Email', dataIndex: 'email', flex: 1 },
        { text: 'Phone', dataIndex: 'phone' }
    ],
    height: 200,
    width: 400,
    renderTo: Ext.getBody()
});

This is how I am trying to pass my grid values to the controller

var records = [];
this.store.data.each(function(rec) {
     records.push(rec.data);
});
Ext.Ajax.request({
     url: 'mycontroller/read_grid',
     params: {
          par1: Ext.encode(records)
     }
})

my problem is how do I get all the values from column phone to update my sql database. Do i use some kind of array or is there a method to get all the values of column "phone" to store in database


Solution

  • You are doing it the wrong way. First you should always have one [idProperty][1] property for a model and you should always use a model! Now if you don't define a model the store will use implicit model and if you don't have defined a idProperty it will be 'id' by default.

    Now let's assume you have all this: If you then add or edit models instances (records) of that type that are bound to a store (added or loaded to it) you can submit all of them that were either changed (have dirty fields) or are new (are phantom) by calling sync() on the store. The proxy will need a appropriate writer for this. In you case you may configure the write with [allowSingle][2] set to false to force the writer to always send a array (list) of records back to the server otherwise you may struggle with deserialize errors on your controller.

    Your case sounds a bit uncommon cause if you just send the phone numbers how can you know which phone number belongs tho whom? Anyway if you do it like described above you have it the right way and you can still decide which fields you take on the server.

    Note: If you define a proxy on the model you don't need to define the same again on the store.

    Update

    To fetch just one field type and send it you can do (untested)

    var emails = store.collect('email');
    Ext.Ajax.request({
        url: 'page.php',
        jsonData: emails,
        success: function(response){
            var text = response.responseText;
            // process server response here
        }
    });
    

    see collect & request for more information