I downloaded two projects today. One is called JsonMVC and the other is called JQueryGridDemo. The former works fine; the problem is it creates data on the fly and populates it in a treeview which I am not using. But it was good for getting my feet wet as I am new to MVC.
If you don't know much about MVC either, that is part of my difficulty. I am learning it, and it is very different from Web Forms. But there are good examples out there, so it is coming along.
The other project, JQueryGridDemo, has a grid - a jquery grid. Ultimately, we will be using a datatable (not a sql server datatable but a datatables.net datatable. It's a design thing that also presents your data in a grid format). I don't really care what we use, all I have to do is return a JSON string of rows of data to the client, and my job is done.
JQueryGridDemo has three examples - a static grid (got that working) and then two different kinds of grids, one uses LINQ one doesn't. But I do not know how to replace this key piece of code to call my own stored procedure:
Code:
public JsonResult DynamicGridData(string sidx, string sord, int page, int rows)
{
var context = new HaackOverflowDataContext();
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
int totalRecords = context.Questions.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
var questions = context.Questions.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize);
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = (
from question in questions
select new
{
i = question.Id,
cell = new string[] { question.Id.ToString(), question.Votes.ToString(), question.Title }
}).ToArray()
};
return Json(jsonData);
}
"from question in questions select new..." - what the heck is that all about?
The static grid data just looks like this:
Code:
public JsonResult GridData(string sidx, string sord, int page, int rows)
{
int totalPages = 1; // we'll implement later
int pageSize = rows;
int totalRecords = 3; // implement later
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = new[]{
new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
}
};
return Json(jsonData);
}
That is simple. But rather...stagnant!