0

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

sra
  • 23,820
  • 7
  • 55
  • 89
EagleFox
  • 1,367
  • 10
  • 34
  • 58

2 Answers2

2

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

sra
  • 23,820
  • 7
  • 55
  • 89
  • I am sorry sra... actually, it's the email that I am sending to my server. basically email addresses are entered on the grid and after I hit the submit button I am getting all the email addresses on the grid to send out emails to those addresses. so would I still need idProperty? – EagleFox Feb 01 '13 at 07:12
  • 1
    @EagleFox OK, then see my edit. I think if the store is just local then this may not be strictly required. Check what I added, I guess you will see if it works. – sra Feb 01 '13 at 07:21
  • yes thank you sra... I can get the emails from client side fine, now how do I process that on the server side... I am sorry I use c#... I tried to make "emails" an object and then get the values, but I get null – EagleFox Feb 01 '13 at 07:28
  • 1
    @EagleFox First check with firebug or chrome console what your client sends back. It should be a array of strings or sort of it. – sra Feb 01 '13 at 07:33
  • thank you sra... It was my mistake... was using the wrong id on serverside... works like a charm... :) – EagleFox Feb 01 '13 at 07:43
0
public class UserController : ControllerBase
{
    private DemoDbContext _dbContext;

    public UserController(DemoDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    [HttpGet("GetUsers")]
    public IActionResult Get()
    {
        var users = _dbContext.demodb.ToList();
        return Ok(users);
    }

    [HttpPost("CreateUser")]
    public IActionResult Create([FromBody] UserRequest request)
    {
        tblDemodb user = new tblDemodb();
        user.UserName = request.UserName;
        user.FirstName = request.FirstName;
        user.LastName = request.LastName;
        user.City = request.City;
        user.State = request.Country;
        user.Country = request.State;
        _dbContext.demodb.Add(user);
        _dbContext.SaveChanges();
        return Ok();
    }

    [HttpPut("UpdateUser")]
    public IActionResult Update([FromBody] UserRequest request)
    {
        var user = _dbContext.demodb.FirstOrDefault(x => x.Id == request.Id);
        user.UserName = request.UserName;
        user.FirstName = request.FirstName;
        user.LastName = request.LastName;
        user.City = request.City;
        user.State = request.Country;
        user.Country = request.State;
        _dbContext.Entry(user).State = EntityState.Modified;
        _dbContext.SaveChanges();
        return Ok(_dbContext.demodb.ToList());
    }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 20 '22 at 21:00