如何调试以下控制器代码以检查pagedData
变量中正在填充什么? pagedData
的类型应为pagedData
,因为这是Pagination.PagedResult
的输出。 pagedData
应包含一个列表和两个整数值。填充pagedData
的内容后如何将其返回到控制台?我无法调试javascript函数,因为它未进入getJSON
部分,问题似乎出在控制器中。理想情况下,我想查看从控制器发送回视图的JSON。
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using CogntionCM.Models;
using CogntionCM.Helper;
namespace CCM.Controllers
{
public class RetailersController : Controller
{
private MCDEntities context = new MCDEntities();
[HttpGet]
public ActionResult Index()
{
return View();
}
public ActionResult GetPagedData(int pageNumber = 1, int pageSize = 20)
{
List<Retailer> listData = context.Retailers.ToList();
var pagedData = Pagination.PagedResult(listData, pageNumber, pageSize);
return Json(pagedData, JsonRequestBehavior.AllowGet);
}
}
}
控制器按如下方式使用两个Helpers
:
PagedData.cs
namespace CCM.Helper
{
public class PagedData<T> where T : class
{
public IEnumerable<T> Data { get; set; }
public int TotalPages { get; set; }
public int CurrentPage { get; set; }
}
}
Pagination.cs
namespace CCM.Helper
{
public static class Pagination
{
public static PagedData<T> PagedResult<T>(this List<T> list, int PageNumber, int PageSize) where T : class
{
var result = new PagedData<T>();
result.Data = list.Skip(PageSize * (PageNumber - 1)).Take(PageSize).ToList();
result.TotalPages = Convert.ToInt32(Math.Ceiling((double)list.Count() / PageSize));
result.CurrentPage = PageNumber;
return result;
}
}
}
以下是零售商模式:
namespace CCM.Models
{
using System;
using System.Collections.Generic;
public class Retailer
{
public int RetailerID { get; set; }
public string RetailerCode { get; set; }
public string Name { get; set; }
...lots more columns...
public virtual Retailer_Statuses Retailer_Statuses { get; set; }
public virtual Retailer_Types Retailer_Types { get; set; }
}
}
以下JavaScript位于“零售商/索引”页面上:
$(document).ready(function () {
GetPageData(1, 20);
});
function GetPageData(pageNum, pageSize) {
$("#tblData").empty();
$("#paged").empty();;
$.getJSON("/Retailers/Index/GetPagedData", { pageNumber: pageNum, pageSize: pageSize }, function (response) {
var rowData = "";
for (var i = 0; i < response.Data.length; i++) {
rowData = rowData + '<tr><td>' + response.Data[i].Name + '</td><td>' + response.Data[i].RetailerCode + '</td></tr>';
}
$("#tblData").append(rowData);
PagingTemplate(response.TotalPages, response.CurrentPage);
});
};