Quantcast
Viewing all articles
Browse latest Browse all 9386

ASP.Net Core Razor Pages - creating a method to return list as json

I have a ASP.Net Core Razor pages project. Trying to implement a DataTables. The HTML table was created through the scaffolding process and uses foreach loop to load the model data.

Here is my javascript to try and implement the datatable.

@section scripts {<script type="text/javascript">$(function () {$.ajax({
                    type: 'GET',
                    url: '@Url.Page("TicketQuery", "DataSet")',
                    data: '{}',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: OnSuccess,
                    failure: function (response) {
                        alert(response.d);
                        alert(response);
                    },
                    error: function (response) {
                        alert(response.d);
                        alert(response);
                    }
                });
        });

        function OnSuccess(response) {
                $("#myTable").DataTable(
                    {
                        bLengthChange: true,
                        lengthMenu: [[5, 10, -1], [5, 10, "All"]],
                        bFilter: true,
                        bSort: true,
                        bPaginate: true,
                        data: response,
                        columns: [{ data: 'LocationName' },
                        { data: 'ScaleTicketId' },
                        { data: 'SettlementStatus' },
                        { data: 'SettlementNbr' },
                        { data: 'VehicleId' },
                        { data: 'VendorId' },
                        { data: 'VendorFullName' },
                        { data: 'CommodityId' },
                        { data: 'SplitPercentage' },
                        { data: 'SplitWeight' },
                        { data: 'UnappliedQuantity' },
                        { data: 'AppliedQuantity' },
                        { data: 'ShrinkQuantity' },
                        { data: 'Grade' },
                        { data: 'TicketReference' },
                        { data: 'LoadOutDate' },
                        { data: 'TicketDate' },
                        { data: 'TicketType' },
                        { data: 'ContractType' },
                        { data: 'PricingTypeCode' },
                        { data: 'ContractId' },
                        { data: 'ShippedFromName' },
                        { data: 'ShippedToName' },
                        { data: 'IntendedLocationName' }]

                    });

            };

    </script>
}

I think I need a method that converts a list and returns Json to implement the datatable. Not sure how to go about this.  My code below returns a string which doesn't work with IActionResult. I know it is not at all correct but shows what I am trying to do. 

public IActionResult OnGetDataSet()
{

CornerstoneTicketQuery =  _context.Cornerstone_Ticket_Query.FromSqlRaw("cornerstone_get_tickets_query {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}",
CustomerId, CommodityId, LocationId, TicketType, SettlementStatus, PricingType, ContractId, ContractType, FromDate, ToDate, TicketId).AsNoTracking().ToList();


return Newtonsoft.Json.JsonConvert.SerializeObject(CornerstoneTicketQuery);
            

}

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>