ODataV4Adapter Batch Backend Example?

After rummaging through the documentation, I found where it shows an example on how to "Persist data in the server" as it relates to Batching.  However, the example uses a UrlAdapter rather than an ODataV4Adapter.  I'm not able to create a controller method that binds correctly for the action.

Here is an example of what the frontend is trying to call:

https://localhost:7189/grid/queryodata?gridId=5&instanceId=d9a2bc43-0df8-4f0f-6550-08da9fecbaac&select=firstName,lastName,jobTitle,emailAddress,phoneNumber,phoneExt/$batch

And here is an example of my backend controller action that works (without the body):

[HttpPost("queryodata")]
public IActionResult BatchOData(ODataQueryOptions<dynamic> options, int gridId, Guid? instanceId, int userId = 1)
{
return Ok();
}

How would I need to alter the controller action to accept the body or "changes" being posted?

I tried adding a "[FromBody] object contents", to the parameter listing but it would not bind properly.

Thanks,

- JV


1 Reply

RS Rajapandiyan Settu Syncfusion Team September 28, 2022 01:49 PM UTC

Hi Jeff,


Thanks for contacting Syncfusion support.


The ODataV4Adaptor is used to fetch the data from Odata standard services. Kindly refer to the below documentation for more information.


OdataV4: http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part1-protocol/odata-v4.0-errata03-os-part1-protocol-complete.html#_Toc453752197

OdataV4Adaptor: https://ej2.syncfusion.com/react/documentation/grid/data-binding/remote-data/#odata-v4-adaptor---binding-odata-v4-service


 

    public gridData: DataManager = new DataManager({

                url: "/odata/Books",

                adaptor: new ODataV4Adaptor()

            });

 


By default, If we use ODataV4Adaptor, the GET method will be triggered for all the grid actions like Paging, Sorting, Filtering, etc., The POST, PATCH, DELETE methods will be triggered for CRUD actions like insert, edit, delete respectively. In these methods, you can perform the CRUD actions in your service.


Screenshot: payload on CRUD actions


 

namespace Batch_OdataV4_Core.Controllers

{

    [Route("api/[controller]")]

    public class BooksController : ODataController

    {

        private BookStoreContext _db;

 

 

        public BooksController(BookStoreContext context)

        {

            _db = context;

            if (context.Books.Count() == 0)

            {

                foreach (var b in DataSource.GetBooks())

                {

                    context.Books.Add(b);

                }

                context.SaveChanges();

            }

 

        }

        // GET api/values

        [HttpGet]

        [EnableQuery]

        public IQueryable<Book> Get()

        {

            return _db.Books;

        }

 

        // POST api/values

        [HttpPost]

        [EnableQuery]

        public IActionResult Post([FromBody]Book book)

        {

            _db.Books.Add(book);

            _db.SaveChanges();

            return Created(book);

        }

 

        [System.Web.Http.AcceptVerbs("PATCH", "MERGE")]

        public Book Patch(int key, [FromBody]Delta<Book> patch)

        {

            var query = Request.Headers["params"];

            var entity = _db.Books.Find(key);

            patch.Patch(entity);

            _db.SaveChanges();

            return entity;

        }

 

        // DELETE api/values/5

        [HttpDelete("{id}")]

        public async Task<int> Delete(int key)

        {

            var od = await _db.Books.FindAsync(key);

 

            _db.Books.Remove(od);

            await _db.SaveChangesAsync();

            return key;

        }

    }

}

 


If you want to bind SQL data or List type of data to the Grid, you can use URL Adaptor data-binding for this.


URL Adaptor: https://ej2.syncfusion.com/react/documentation/grid/editing/persisting-data-in-server/#using-url-adaptor

Please get back to us if you need further assistance.


Regards, 

Rajapandiyan S 


Loader.
Up arrow icon