Вот мой код на стороне клиента:
$("#grid").jqGrid({
url: "/Entry/EntryListingByBreed/",
datatype: "json",
mtype: "POST",
postData: { showId: showId, breedId: breedId },
colNames: ["Class", "NZKC Reg", "Registered name", "Date of birth", "Entered"],
colModel: [
{ name: "ClassNumber", sortable: false, width: 50 },
{ name: "NZKCRegistration", sortable: false, width: 100 },
{ name: "RegisteredName", sortable: false, width: 200 },
{ name: "DateOfBirth", sortable: false, width: 200 },
{ name: "Entered", sortable: false, width: 200 }
],
pager: jQuery("#pager"),
rowNum: 20,
rowList: [10, 20, 40],
altRows: true,
altclass: "gridAltRow",
viewrecords: true,
caption: "Entries for " + breedName
});
, а вот мой код на стороне сервера:
[RequiresAuthentication]
[HttpPost]
public ActionResult EntryListingByBreed(int showId, int breedId, string sidx, string sord, int page, int rows)
{
using (var dataContext = new NZDogShowsEntities())
{
int pageIndex = page - 1;
int pageSize = rows;
var entries = from se in dataContext.ShowEntries
where se.ShowID == showId && se.Entry.BreedID == breedId
select se;
int totalRecords = entries.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
var showEntries = (from se in dataContext.ShowEntries.AsEntryListingAggregate().AsEnumerable()
where se.ShowID == showId && se.Entry.BreedID == breedId
select new
{
ID = se.ID,
ClassNumber = se.Class.Number.ToString(),
NZKCRegistration = se.Entry.NZKCRegNumber,
RegisteredName = se.Entry.RegisteredName,
DateOfBirth = se.Entry.DateOfBirth.GetValueOrDefault().ToLongDateString(),
Entered = se.Entry.DateOfBirth.GetValueOrDefault().ToLongDateString()
}).ToList();
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from entry in showEntries
select new
{
i = entry.ID,
cell = new string[]{
entry.ClassNumber,
entry.NZKCRegistration,
entry.RegisteredName,
entry.DateOfBirth,
entry.Entered
}
}).ToArray()
};
var viewData = Json(jsonData);
return viewData;
}
}
}
Мой код на стороне сервера выглядит более многословно, чем необходимо, ноэто было хорошо, пока я решаю проблемы.Переменная viewData
показывает все нужные данные, количество строк, номер страницы и т. Д. И т. Д.
Сетка отформатирована, т.е.отображение всей хромированной окантовки и т. д. Однако данные не отображаются.Даже «Страница x of y» не верна.Он показывает «Страница 0 из» и <span>
, где общее количество записей должно отображаться, пусто.Я в растерянности - возможно, упускаю что-то ужасно очевидное, тем более что я новичок во всем этом.
Любая помощь / указатели (предназначенные для каламбура) будут с благодарностью.
Спасибо.
РЕДАКТИРОВАТЬ: Хорошо, вышеуказанный код на стороне сервера был исправлен и теперь генерирует строку JSON.Кажется, он правильно отформатирован.Я не буду обновлять приведенный выше код, но приведу ниже строку JSON.Он анализирует правильно, но Firebug по-прежнему сообщает «недопустимая метка».Сетка тоже не заполняется!Вот строка - я не могу определить для меня проблему:
{"total":2,"page":1,"records":25,"rows":[{"id":2,"cell":["2a","1234567","Test Dog","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":3,"cell":["4a","abcdef","Test Dog 2","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":4,"cell":["5a","abc123","Test Dog 3","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":5,"cell":["6a","asdf890","Test Dog X","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":6,"cell":["6","qwerty","Test Dog Y","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":7,"cell":["1","fw3asd","Test Dog 4","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":8,"cell":["10","asdfa","Test Dog 5","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":9,"cell":["11a","houh2","Test Dog 6","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":11,"cell":["8","xxxx","Test X","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":12,"cell":["8","zzzz","Test Z","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":13,"cell":["1","qqqq","Test Q","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":14,"cell":["8a","tttt","Test T","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":15,"cell":["8a","rrrr","Test R","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":16,"cell":["7","aaaa","Test A","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":17,"cell":["1","11223344","Test Entry","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":18,"cell":["1","66778899","asdfa","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":19,"cell":["1","13o24bo","qwlen;oq","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":20,"cell":["1","oubiou","asdfa","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":21,"cell":["1","o2no3","hohohop","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":22,"cell":["1","o48t2","Test 09808","Monday, 1 January 0001","Monday, 1 January 0001"]}]}