获得使用参数返回特定可查询项的ActionResult时遇到问题

时间:2016-04-11 18:25:25

标签: asp.net-mvc telerik-mvc

我试图让这个动作结果使用给定的参数(Year,Qtr,Div)并仅返回这些结果。我在其他ActionResults中使用与此类似的东西,但它们不会在Json中返回结果。我不太清楚我需要做什么才能使这个结果只返回与给定参数匹配的结果。它正在做的是返回所有记录,而不是通过参数过滤它们。

    public ActionResult GLAcctsISDs_Read([DataSourceRequest]  DataSourceRequest request, int? Year, int? Qtr, string Div)
    {
        var glacctsisds = db.GLAcctsISDs.AsQueryable();
        if (Year.HasValue)
            glacctsisds = glacctsisds.Where(x => x.Year == Year);
        if (Qtr.HasValue)
            glacctsisds = glacctsisds.Where(x => x.Qtr == Qtr);
        if (!string.IsNullOrEmpty(Div))
            glacctsisds = glacctsisds.Where(x => x.Div == Div);

        DataSourceResult result = glacctsisds.ToDataSourceResult(request, gLAcctsISD => new {

            ID = gLAcctsISD.ID,
            Div = gLAcctsISD.Div,
            Year = gLAcctsISD.Year,
            Qtr = gLAcctsISD.Qtr,
            GLAcct = gLAcctsISD.GLAcct,
            GLDescr = gLAcctsISD.GLDescr,
            TBDebit = gLAcctsISD.TBDebit,
            TBCredit = gLAcctsISD.TBCredit,
            IncExpAdjDebit = gLAcctsISD.IncExpAdjDebit,
            IncExpAdjCredit = gLAcctsISD.IncExpAdjCredit,
            AdlTrialDebit = gLAcctsISD.AdlTrialDebit,
            AdjTrialCredit = gLAcctsISD.AdjTrialCredit,
            RemainExpDebit = gLAcctsISD.RemainExpDebit,
            PendingIncomeCredit = gLAcctsISD.PendingIncomeCredit,
            ProjRemainExpDebit = gLAcctsISD.ProjRemainExpDebit,
            NetIncomeCredit = gLAcctsISD.NetIncomeCredit,
            DivGLNotes = gLAcctsISD.DivGLNotes
        });


        return Json(result);
    }

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

尝试将“result”对象序列化为json。

string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(result);

return Json(jsonString);

答案 1 :(得分:0)

对于想要知道如何做到这一点的人,你去: 参数以year,qtr和div传递给控制器​​:

 public ActionResult KenodGLAcctISDList( int? year, int? qtr, string div)
    {
        ViewBag.YYear = year;
        ViewBag.QQtr = qtr;
        ViewBag.DDiv = div;
        return View();
    }

    public ActionResult GLAcctsISDs_Read([DataSourceRequest]DataSourceRequest request)
    {

        var glacctsisds = db.GLAcctsISDs.AsQueryable();
        DataSourceResult result = glacctsisds.ToDataSourceResult(request, gLAcctsISD => new {

            ID = gLAcctsISD.ID,
            Div = gLAcctsISD.Div,
            Year = gLAcctsISD.Year,
            Qtr = gLAcctsISD.Qtr,
            GLAcct = gLAcctsISD.GLAcct,
            GLDescr = gLAcctsISD.GLDescr,
            TBDebit = gLAcctsISD.TBDebit,
            TBCredit = gLAcctsISD.TBCredit,
            IncExpAdjDebit = gLAcctsISD.IncExpAdjDebit,
            IncExpAdjCredit = gLAcctsISD.IncExpAdjCredit,
            AdlTrialDebit = gLAcctsISD.AdlTrialDebit,
            AdjTrialCredit = gLAcctsISD.AdjTrialCredit,
            RemainExpDebit = gLAcctsISD.RemainExpDebit,
            PendingIncomeCredit = gLAcctsISD.PendingIncomeCredit,
            ProjRemainExpDebit = gLAcctsISD.ProjRemainExpDebit,
            NetIncomeCredit = gLAcctsISD.NetIncomeCredit,
            DivGLNotes = gLAcctsISD.DivGLNotes
        });


        return Json(result);
    }

然后作为Viewbags传递给视图,并按KendoUI网格代码

排序
.Read(read => read.Action("GLAcctsISDs_Read", "KendoGLAcctISD"))
      .Filter(filter =>
              {
                  filter.Add(f => f.Year).IsEqualTo(ViewBag.YYear);
                  filter.Add(f => f.Qtr).IsEqualTo(ViewBag.QQtr);

              })
      .Update(update => update.Action("GLAcctsISDs_Update", "KendoGLAcctISD"))