如何将jqGrid代码从控制器的JsonResult方法重构为松耦合模型?

时间:2015-04-14 15:19:48

标签: c# jqgrid asp.net-mvc-5 decoupling cohesion

我正在使用Code Project: ASP.NET MVC-4,Entity Framework and JQGrid Demo with simple Todo List WebApplication中的示例。但是,由于控制器正在执行所有业务逻辑,因此开发人员没有最大化内聚力。我试图将JsonResult方法与控制器分离,并创建松散耦合的类来处理模型中的所有业务逻辑。

这是我的原始代码:

[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);
    }

开始分解逻辑,在控制器中,我创建了几个类:

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; }
}

然后下面的类调用该方法返回数据:

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;
    }
}

现在,在控制器中,我应该可以让控制器只调用:

[HttpPost]
    public JsonResult getPriceTable(string sidx, string sord, int page, int rows)
    {

        return Json(adminPriceTableList.priceTableResult(sidx, sord,page,rows), JsonRequestBehavior.AllowGet);
    }

但是,jqGrid没有填充。当控制器处理所有逻辑时,我怀疑它与原始代码有关(如下所示)。

var jsonData = new
        {
            total = totalPages,
            page,
            records = totalRecords,
            rows = modelPrices
        };

有人可以帮我理解我做的不正确吗?你看到我哪里出错或者我没有采用正确的方法吗?

感谢。

1 个答案:

答案 0 :(得分:0)

审核完代码后,我没注意到 modelPriceList

public List<adminPriceTable> modelPricesList = new List<adminPriceTable>

必须与 modelPrices

同名
 var jsonData = new
    {
        total = totalPages,
        page,
        records = totalRecords,
        rows = modelPrices
    };

所以在重命名列表....

 public List<adminPriceTable> modelPrices = new List<adminPriceTable>

...产生结果。