About 'Draw' in relation to data table

About 'Draw' in relation to data table

jt4000jt4000 Posts: 7Questions: 4Answers: 0

Hello,

I do not understand the instruction of 'draw'.

How should I get the data for "draw" in relation to returned data?

(https://datatables.net/manual/server-side#Sent-parameters)

Thank you!

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using RestSharp;
using RestSharp.Authenticators;
using Newtonsoft.Json;


namespace CloudTableApi.Controllers

{
    [ApiController]
    [Route("api/[controller]")]
    public class TestsController : ControllerBase
    {
        /// <summary> 
        /// https://localhost:44340/api/Tests/ 
        /// </summary> 
        /// <returns></returns> 
        [HttpGet("GetAllPosts")]
        public Info GetAllPosts()
        {
            var client = new RestClient("https://jsonplaceholder.typicode.com/");
            var request = new RestRequest("comments", DataFormat.Json);
            var response = client.Get(request);

            List<Comment> m = JsonConvert.DeserializeObject<List<Comment>>(response.Content);

            Info my = new Info();
            my.Data = m;

            return my;
        }


        /// <summary> 
        /// https://localhost:44340/api/Tests/SimpleGetAllPostsByPagenumber?pagenumber=1 
        /// </summary> 
        /// <param name="pagenumber"></param> 
        /// <returns></returns> 
        [HttpGet("SimpleGetAllPostsByPagenumber")]
        public List<Comment> SimpleGetAllPostsByPagenumber(int pagenumber)
        {
            var clientAllData = new RestClient("https://jsonplaceholder.typicode.com/");
            var requestAllData = new RestRequest("comments", DataFormat.Json);
            var responseAllData = clientAllData.Get(requestAllData);

            List<Comment> datawithpagenumberAllData = JsonConvert.DeserializeObject<List<Comment>>(responseAllData.Content);


            //---------- 

            var client = new RestClient("https://jsonplaceholder.typicode.com/");
            var request = new RestRequest("comments?postId=" + pagenumber.ToString(), DataFormat.Json);
            var response = client.Get(request);
            List<Comment> datawithpagenumber = JsonConvert.DeserializeObject<List<Comment>>(response.Content);

            //---------- 



            /* 

            Info info = new Info(); 

            info.Data = datawithpagenumber; 

            info.TotalRecords = datawithpagenumberAllData.Count(); 

            info.TotalDisplayRecords = datawithpagenumber.Count(); 

            */


            return datawithpagenumber;
        }


        /// <summary> 
        /// https://localhost:44340/api/Tests/GetAllPostsByPagenumber?pagenumber=1 
        /// </summary> 
        /// <param name="pagenumber"></param> 
        /// <returns></returns> 
        [HttpGet("GetAllPostsByPagenumber")]
        public Info GetAllPostsByPagenumber(int pagenumber)
        {
            var clientAllData = new RestClient("https://jsonplaceholder.typicode.com/");
            var requestAllData = new RestRequest("comments", DataFormat.Json);
            var responseAllData = clientAllData.Get(requestAllData);

            List<Comment> datawithpagenumberAllData = JsonConvert.DeserializeObject<List<Comment>>(responseAllData.Content);



            //---------- 

            var client = new RestClient("https://jsonplaceholder.typicode.com/");
            var request = new RestRequest("comments?postId=" + pagenumber.ToString(), DataFormat.Json);
            var response = client.Get(request);

            List<Comment> datawithpagenumber = JsonConvert.DeserializeObject<List<Comment>>(response.Content);

            //---------- 

            Info info = new Info();
            info.Data = datawithpagenumber;
            info.recordsTotal = datawithpagenumberAllData.Count();
            info.recordsFiltered = datawithpagenumber.Count();

            return info;
        }





    }


    public class Info
    {
        public int recordsTotal { get; set; }
        public int recordsFiltered { get; set; }
        public List<Comment> Data { get; set; }
    }


    public class Comment
    {
        public int PostId { get; set; }
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Body { get; set; }
    }

}

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    The draw sent in the request must be returned in the response - this allows the client to ensure the response matches the request,

    Colin

  • jt4000jt4000 Posts: 7Questions: 4Answers: 0

    How do I get the data of draw in frontend?

  • allanallan Posts: 61,611Questions: 1Answers: 10,089 Site admin

    Why would you want the draw parameter on the client-side? It is used internally in DataTables to make sure Ajax requests are sequential (since Ajax can return them out of order).

    From your other threads you are using query parameters for the data being sent to the server, so you would get the draw parameter on the server-side the same way as you would any other query parameter: Request.Query["draw"].

    Allan

Sign In or Register to comment.