ShaneBrennan
Programmer
Not sure if this is the correct place for this.
I've got a working procedure to export the contents of a DB table to json - but it's fixed on a sort field and order. What I want to do it take 2 parameters (sent in from Wijmo Grid) to sort a named field (sortColumn) into ascending or descending order (using sortDirection). Here is the code:
public JsonResult GetClientList()
{
int pageSize = Request.Params["paging[pageSize]"] != null ? Convert.ToInt32(Request.Params["paging[pageSize]"]) : 0;
int pageIndex = Request.Params["paging[pageIndex]"] != null ? Convert.ToInt32(Request.Params["paging[pageIndex]"]) : 0;
string sortColumn = Request.Params["sorting[0](dataKey)"];
string sortDirection = Request.Params["sorting[0](sortDirection)"];
if (string.IsNullOrEmpty(sortColumn)) sortColumn = "Name";
if (string.IsNullOrEmpty(sortDirection)) sortDirection = "descending";
var allClients = _db.Clients
.OrderBy(r => r.Name)
.Select(r => new ClientListViewModel
{
ID = r.ID,
Name = r.Name,
Street = r.Street,
TownCity = r.TownCity,
County = r.County,
Contact = r.Contact,
Status = r.Status,
Switchboard = r.Switchboard,
NoClientContacts = r.ClientContacts.Count()
});
var totalRowCount = allClients.Count();
if (pageSize == 0)
pageSize = totalRowCount;
allClients = allClients.OrderBy(r => r.Name);
allClients = allClients.Skip(pageSize * pageIndex).Take(pageSize);
var result = new wijmoGridResult<ClientListViewModel> { Items = allClients.ToList(), TotalRowCount = totalRowCount };
return Json(result, JsonRequestBehavior.AllowGet);
}
Thank you in advance to any help you can offer.
Shane Brennan
I've got a working procedure to export the contents of a DB table to json - but it's fixed on a sort field and order. What I want to do it take 2 parameters (sent in from Wijmo Grid) to sort a named field (sortColumn) into ascending or descending order (using sortDirection). Here is the code:
public JsonResult GetClientList()
{
int pageSize = Request.Params["paging[pageSize]"] != null ? Convert.ToInt32(Request.Params["paging[pageSize]"]) : 0;
int pageIndex = Request.Params["paging[pageIndex]"] != null ? Convert.ToInt32(Request.Params["paging[pageIndex]"]) : 0;
string sortColumn = Request.Params["sorting[0](dataKey)"];
string sortDirection = Request.Params["sorting[0](sortDirection)"];
if (string.IsNullOrEmpty(sortColumn)) sortColumn = "Name";
if (string.IsNullOrEmpty(sortDirection)) sortDirection = "descending";
var allClients = _db.Clients
.OrderBy(r => r.Name)
.Select(r => new ClientListViewModel
{
ID = r.ID,
Name = r.Name,
Street = r.Street,
TownCity = r.TownCity,
County = r.County,
Contact = r.Contact,
Status = r.Status,
Switchboard = r.Switchboard,
NoClientContacts = r.ClientContacts.Count()
});
var totalRowCount = allClients.Count();
if (pageSize == 0)
pageSize = totalRowCount;
allClients = allClients.OrderBy(r => r.Name);
allClients = allClients.Skip(pageSize * pageIndex).Take(pageSize);
var result = new wijmoGridResult<ClientListViewModel> { Items = allClients.ToList(), TotalRowCount = totalRowCount };
return Json(result, JsonRequestBehavior.AllowGet);
}
Thank you in advance to any help you can offer.
Shane Brennan