javascript - Return object with IEnumerable with AJAX -
i have problem while getting object controller via ajax call.to precise, object contains property ienumerable type.
class 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; } }
class 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; } }
this controller execute call :
[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 chart data resultchart = cp in db.customerproducts join pr in db.products on cp.productid equals pr.productid group cp cp.product.productdescription grp select new ereport.chartitem { label = grp.key, value = grp.count()}; break; case ereport.reportname.crcustomerproductappmasterpivot: //reload chart data resultchart = cp in db.customerproducts join pr in db.products on cp.productid equals pr.productid group cp cp.product.productdescription 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"}); } }
and ajax call :
$.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); } });
actually go through each record.chartitem's object , process there. somehow returned record not being recognized. below error :
"typeerror: result.record.chartitem undefined".
may know correct way list of data using ajax ?
thanks lot
change success function below , try
success: function (result) { var len = result.record.chartitems.length; },
you have misspelled property chartitems
. think work
Comments
Post a Comment