Datatable editor posting to db

Datatable editor posting to db

aelghaelgh Posts: 6Questions: 4Answers: 0

Hello all!

I have the editor extension to my datatable. When a cell is changed I want the value to be sent to a service --> controller --> repository and then be updated via an SP.

Columns in db are, Dog, Cat, Fish, Cow.

JS:

 $(document).ready(function () {
                editor = new $.fn.dataTable.Editor( {
                    ajax: {
                        url: "Services/FileName.svc/SaveNewAnimals ",
                        contentType: 'text/javascript; charset=utf-8',
                        dataType: 'json',
                        data: function(d){
                        
                           return JSON.stringify(d.data);
                       },
                    },

d.data will no contain the value that has been changed in the table. For example lets say we change a 1 --> 2.
d.data will look like this: 17058: {Cow: "2"} - if you change a cell in the cow column. Also the number 17058 I have no idea what that is.

Service (c#):

 public propertyClass SaveNewAnimals (arg) 
        {

            var response = new AnimalPropertys
            {
                Cow = arg.Cow
            };
            Controller.AnimalMethodInController(response);
            return response;
        }

The problem is the **argument **in the service. When I POST from js --> service and send the data 17058: {Cow: "2"} I dont know how to retrieve it with an argument. Shall I create a property class for the send POST and map the values with the properties. Or can I simply send a int from the POST?

What is the best way to retrieve the POST data in a method? Which datatype can I put as and argument to retrieve the data being send from POST.

Br,

Anton

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin
    Answer ✓

    Also the number 17058 I have no idea what that is.

    That's the primary key (i.e. the id) for the row.

    If you have the client-side sending raw JSON as the request body, you could use Newtonsoft JSON.net to decode that and then loop over it processing the data.

    Another option, if you aren't bothered about Editor's multi-row editing abilities (which don't work with inline editing btw) then you could flatten the data on the client-side:

    ajax: {
      ...,
      data: function ( d ) {
        var ret = {};
        $.each( d.data, function ( val, key ) {
          $.extend( ret, val );
          ret.id = key;  
        } );
    
        return ret;
      }
    }
    

    Allan

This discussion has been closed.