I'm using an example from Code Project: ASP.NET MVC-4,Entity Framework and JQGrid Demo with simple Todo List WebApplication. However, the developer didn't maximize cohesion since the the controller is doing all the business logic. I'm trying to decouple the JsonResult method, from the controller, and create loosely coupled classes that handle all the business logic from the model.
Here is my original code:
[HttpPost]
public JsonResult getPriceTable(string sidx, string sord, int page, int rows)
{
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
var modelPrices = db.VW_MODELS_CHARGEs
.OrderBy(x => x.SERIAL_CODE)
.Where(x => x.ACTIVE == 1)
.Select(x => new { x.VW_MC_OBJID, x.S_MODEL, x.CHARGE, x.SERIAL_CODE });
int totalRecords = modelPrices.Count();
var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = modelPrices
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
to begin breaking up the logic, in the controller, I created several classes:
public class jqGridTable
{
public int pageIndex { get; set; }
public int pageSize { get; set; }
public int totalRecords { get; set; }
public int totalPages { get; set; }
public List<adminPriceTable> modelPricesList = new List<adminPriceTable>();
}
public class adminPriceTable
{
public int VW_MC_OBJID { get; set; }
public string S_MODEL { get; set; }
public string SERIAL_CODE { get; set; }
public decimal? CHARGE { get; set; }
}
and then the class below invokes the method to return the data:
public class adminPriceTableList
{
public static jqGridTable priceTableResult(string sidx, string sord, int page, int rows)
{
var jqGrid = new jqGridTable();
jqGrid.pageIndex = Convert.ToInt32(page) - 1;
jqGrid.pageSize = rows;
jqGrid.modelPricesList = NREContext.dbConn.VW_MODELS_CHARGEs
.OrderBy(x => x.SERIAL_CODE)
.Where(x => x.ACTIVE == 1)
.Select(x => new adminPriceTable
{
VW_MC_OBJID = x.VW_MC_OBJID,
SERIAL_CODE = x.SERIAL_CODE,
S_MODEL = x.S_MODEL,
CHARGE = x.CHARGE
}).ToList();
jqGrid.totalRecords = jqGrid.modelPricesList.Count();
jqGrid.totalPages = (int)Math.Ceiling((float)jqGrid.totalRecords / (float)rows);
return jqGrid;
}
}
so now, in the controller, I should be able to have the controller just invoke:
[HttpPost]
public JsonResult getPriceTable(string sidx, string sord, int page, int rows)
{
return Json(adminPriceTableList.priceTableResult(sidx, sord,page,rows), JsonRequestBehavior.AllowGet);
}
However, the jqGrid is not getting populated. I suspect it has something to do with (shown below) from the original code when the controller was handling all the logic.
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = modelPrices
};
Can someone help me understand what I didn't do correctly? Do you see where I went wrong or where I'm not invoking the proper methods?
Thanks.
After reviewing the code, I failed to notice that modelPriceList:
public List<adminPriceTable> modelPricesList = new List<adminPriceTable>
needs to be the same name as modelPrices
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = modelPrices
};
so after renaming the list....
public List<adminPriceTable> modelPrices = new List<adminPriceTable>
...produces results.