using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using NorthwindRazorAPI.Domain; using NorthwindRazorAPI.BusinessObject; namespace NorthwindRazor.Pages { public class Products_ListForeachModel : PageModel { public List<Products> ProductsData { get; set; } public string[,] ProductsFieldNames { get; set; } public string FieldToSort { get; set; } public string FieldToSortWithOrder { get; set; } public string FieldSortOrder { get; set; } public int StartPage { get; set; } public int EndPage { get; set; } public int CurrentPage { get; set; } public int NumberOfPagesToShow { get; set; } public int TotalPages { get; set; } public List<string> UnsortableFields { get; set; } /// <summary> /// Initial handler the razor page encounters. /// </summary> public void OnGet() { string sidx = String.Empty; string sord = String.Empty; int currentPage = 1; GetData(sidx, sord, currentPage); } /// <summary> /// Handler, deletes a record. /// </summary> public IActionResult OnGetRemove(int id) { Products.Delete(id); return new JsonResult(true); } public void OnGetGridData(string sidx, string sord, int? _page) { GetData(sidx, sord, _page); } public void GetData(string sidx, string sord, int? _page) { int rows = Functions.GetGridNumberOfRows(); int numberOfPagesToShow = Functions.GetGridNumberOfPagesToShow(); int currentPage = _page is null ? 1 : Convert.ToInt32(_page); int startRowIndex = ((currentPage * rows) - rows); int totalRecords = Products.GetRecordCount(); int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows); List<Products> objProductsCol = Products.SelectSkipAndTake(rows, startRowIndex, sidx + " " + sord); // fields and titles string[,] fieldNames = new string[,] { {"ProductID", "Product ID"}, {"ProductName", "Product Name"}, {"SupplierID", "Supplier ID"}, {"CategoryID", "Category ID"}, {"QuantityPerUnit", "Quantity Per Unit"}, {"UnitPrice", "Unit Price"}, {"UnitsInStock", "Units In Stock"}, {"UnitsOnOrder", "Units On Order"}, {"ReorderLevel", "Reorder Level"}, {"Discontinued", "Discontinued"} }; // assign properties ProductsData = objProductsCol; ProductsFieldNames = fieldNames; TotalPages = totalPages; CurrentPage = currentPage; FieldToSort = String.IsNullOrEmpty(sidx) ? "ProductID" : sidx; FieldSortOrder = String.IsNullOrEmpty(sord) ? "asc" : sord; FieldToSortWithOrder = String.IsNullOrEmpty(sidx) ? "ProductID" : (sidx + " " + sord).Trim(); NumberOfPagesToShow = numberOfPagesToShow; StartPage = Functions.GetPagerStartPage(currentPage, numberOfPagesToShow, totalPages); EndPage = Functions.GetPagerEndPage(StartPage, currentPage, numberOfPagesToShow, totalPages); } } }