Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export to json - procedure with 2 parameters for sort field and order

Status
Not open for further replies.

ShaneBrennan

Programmer
May 19, 1999
198
GB
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


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top