使用AJAX返回IEnumerable对象

时间:2014-06-03 03:35:22

标签: javascript ajax asp.net-mvc-4

通过AJAX调用从控制器获取对象时遇到了一些问题。确切地说,我想获取包含IEnumerable类型属性的对象。

第1课:

public class ChartItem
        {
            public string cName { get; set; }
            public string label { get; set; }
            public decimal value { get; set; }
            public decimal value2 { get; set; }
            public string value2Cur { get; set; }
            public string value2Unit { get; set; }
            public string color { get; set; }
            public string strokeColor { get; set; }
            public string chartTitle { get; set; }
        }

第2课:

public class ReportParameter
        {
            public string ReportName { get; set; }
            public string DateFrom { get; set; }
            public string DateTo { get; set; }
            public string CountryId { get; set; }
            public string RegionId { get; set; }
            public string RepresentativeId { get; set; }
            public string CustomerId { get; set; }
            public ExportFormatType ReportFormat { get; set; }
            public EReport.ChartType ChartType { get; set; }
            public bool EmailFlag { get; set; }
            public IEnumerable<ChartItem> chartItems { get; set; }
        }

这是执行呼叫的控制器:

 [HttpPost]        
            public JsonResult ReloadReportSummary(EReport.ReportParameter rptParam)
            {
                EMAP.WEB_Bootstrap.Helper.ViewHelper viewHelper = new ViewHelper();
                IEnumerable<EReport.ChartItem> resultChart=null;

                try
                {
                    EReport.ReportParameter eRpt = new EReport.ReportParameter();

                    eRpt.ReportName = ((EReport.ReportName)Enum.Parse(typeof(EReport.ReportName), rptParam.ReportName)).ToString();

                    switch ((EReport.ReportName)Enum.Parse(typeof(EReport.ReportName), rptParam.ReportName))
                    {
                        case EReport.ReportName.CRPotentialCustomerList:

                            //reload the chart data 
                            resultChart =
                                from cp in db.CustomerProducts
                                join pr in db.Products on cp.ProductID equals pr.ProductID
                                group cp by cp.Product.ProductDescription into grp
                                select new EReport.ChartItem { label = grp.Key, value = grp.Count()};

                            break;

                        case EReport.ReportName.CRCustomerProductAppMasterPivot:

                            //reload the chart data 
                            resultChart =
                                from cp in db.CustomerProducts
                                join pr in db.Products on cp.ProductID equals pr.ProductID
                                group cp by cp.Product.ProductDescription into grp
                                select new EReport.ChartItem { label = grp.Key, value = grp.Count() };

                            break;
                        default:
                            break;
                    }



eRpt.chartItems = resultChart;
                ---EDITED----
                    var result = eRpt;        
                            return Json(new { Result = "OK", Record = result }, 
    JsonRequestBehavior.AllowGet);

                }

                catch (Exception ex)
                {   
                    return Json(new { Result = "ERROR"});
                }
            }

这是AJAX电话:

$.ajax({
            url: urlReportSummary,
            data: JSON.stringify(rptParam),
            type: 'POST',
            contentType: 'application/json;',
            dataType: 'json',

            success: function (result) {                
                var len = result.Record.chartItem.length;               
            },
            error: function (ex) {
                alert(ex);
            }
        });

实际上我想浏览每个Record.chartItem的对象并在那里做一些处理。但不知何故,返回的记录未被识别。以下是错误:

"TypeError: result.Record.chartItem is undefined".

我可以知道使用AJAX获取数据列表的正确方法是什么?

非常感谢

1 个答案:

答案 0 :(得分:2)

更改成功功能,如下所示,然后尝试

success: function (result) {                
            var len = result.Record.chartItems.length;               
        },

您拼错了财产chartItems。我想现在它会起作用